1

I've been taught two different methods when using GraphQL,

the first method included building out the schema:

    module.exports = buildSchema(`
        type Event {
            _id: ID!
            title: String!
            description: String!
            price: Float!
            date: String!
            creator: User!
         }
    `)

then creating the resolver in a separate file:

    module.exports = {
    events: async () => {
      try {
        const events = await Event.find();
        return events
      } catch (err) {
        throw err;
      }
    }}

the second method looks like this:

    const PostType = new GraphQLObjectType({
        name: 'Post',
            fields: () => ({
            id: { type: GraphQLID },
            title: { type: GraphQLString },
            content: { type: GraphQLString },
            score: { type: GraphQLInt }
            })
        });
    const RootQuery = new GraphQLObjectType({
        name: 'RootQuery',
        fields: {
            posts: {
            type: new GraphQLList(PostType),
            resolve(parent, args) {
                return Post.find();
            }   
        }
    });

i've worked with both ways and they both work, but i'm not really sure which methods I should be using, the first method seems simpler, but the second method seems to enforce graphQL types.

0 Answers0