feat: solution exercise 2
This commit is contained in:
parent
c4b07ea259
commit
45f7debb77
21
src/data.js
21
src/data.js
|
@ -2,17 +2,32 @@ const uuid = require('uuid');
|
|||
|
||||
const stores = [
|
||||
{
|
||||
city: 'Aalst',
|
||||
id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79',
|
||||
name: 'Den Olijfboom',
|
||||
number: 38,
|
||||
postalCode: '9300',
|
||||
street: 'Molenstraat',
|
||||
},
|
||||
{
|
||||
city: 'Aalst',
|
||||
id: '5f2919aa-333a-4745-8166-3002ab30de0e',
|
||||
name: 'Pizza Talia'
|
||||
name: 'Pizza Talia',
|
||||
number: 147,
|
||||
postalCode: '9300',
|
||||
street: 'Sint Jobstraat',
|
||||
}
|
||||
];
|
||||
|
||||
function createStore({ name }) {
|
||||
const newStore = { id: uuid(), name };
|
||||
function createStore({ city, name, number, postalCode, street }) {
|
||||
const newStore = {
|
||||
city,
|
||||
id: uuid(),
|
||||
name,
|
||||
number,
|
||||
postalCode,
|
||||
street,
|
||||
};
|
||||
stores.push(newStore);
|
||||
return newStore;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,35 @@
|
|||
const { UserInputError } = require('apollo-server');
|
||||
const yup = require('yup');
|
||||
const data = require('./data');
|
||||
|
||||
const createStoreSchema = yup.object()
|
||||
.shape({
|
||||
city: yup.string().max(255),
|
||||
name: yup.string().min(1).max(255).required(),
|
||||
number: yup
|
||||
.number()
|
||||
.required()
|
||||
.positive()
|
||||
.integer(),
|
||||
postalCode: yup.string().max(10),
|
||||
street: yup.string().max(255),
|
||||
});
|
||||
|
||||
// Resolvers define the technique for fetching the types in the
|
||||
// schema.
|
||||
module.exports = {
|
||||
Query: {
|
||||
stores: () => data.getStores(),
|
||||
},
|
||||
Mutation: {
|
||||
createStore: async (parent, { input }) => {
|
||||
try {
|
||||
await createStoreSchema.validate(input);
|
||||
} catch (e) {
|
||||
throw new UserInputError('Invalid input.', { validationErrors: e.errors });
|
||||
}
|
||||
const { city, name, number, postalCode, street } = input;
|
||||
return data.createStore({ city, name, number, postalCode, street });
|
||||
}
|
||||
}
|
||||
};
|
|
@ -6,8 +6,12 @@ module.exports = gql`
|
|||
# Comments in GraphQL are defined with the hash (#) symbol.
|
||||
# This "Store" type can be used in other type declarations.
|
||||
type Store {
|
||||
city: String
|
||||
id: String
|
||||
name: String
|
||||
number: Int
|
||||
postalCode: String
|
||||
street: String
|
||||
}
|
||||
|
||||
# The "Query" type is the root of all GraphQL queries.
|
||||
|
@ -15,4 +19,16 @@ module.exports = gql`
|
|||
type Query {
|
||||
stores: [Store]
|
||||
}
|
||||
|
||||
input StoreInput {
|
||||
city: String
|
||||
name: String
|
||||
number: Int
|
||||
postalCode: String
|
||||
street: String
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createStore(input: StoreInput): Store
|
||||
}
|
||||
`;
|
Loading…
Reference in New Issue