javascriptnode.jsexpressgraphql

Which one should I use when defining schema and resolvers in GraphQl


I saw different ways of using schema and resolvers in graphql with expressjs. Which one should I use, and which one is recommended?

1:

const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: "HelloWorld",
        fields: () =>({
            message: {
                type: GraphQLString,
                resolve: () => "Hello World"
            }
        })
    })
})

2:

const schema = buildSchema(
    type RootQuery{
        message: String!
    }
    Query {
        query: RootQuery
    }
);

const resolver = {
    message: () => "Hello World"
}

i'm using this way when defining schema and resolvers.

const schema = buildSchema(
    type RootQuery{
        message: String!
    }
    Query {
        query: RootQuery
    }
);

const resolver = {
    message: () => "Hello World"
}


Solution

  • Asking which you should use is an opinion-based question, which isn't permitted on StackOverflow. We don't know what you should do. Each of these patterns has their own trade-offs.

    To start you on your path for that answer, I recommend searching the web for "schema-first" vs "code-only" (sometimes "code-first") blog posts. Your first example is "code-only", and your second example is "schema-first"