0

I'm learning graphQl and I saw that there are multiple formats to write schemas. There is the "JavaScript" format:

const rootQuery = new GraphQLObjectType({
    name:"root",
    description:"root of the resources",
    fields: () => ({
        user: ({
            type:userType,
            args:{
                id: {type:GraphQLInt}
            }
......

and the SDL format:

type Query {
  me: User
}

type User {
  id: ID
  name: String
}
.....

In GQL docs I see the SDL format, but in every other source I see the JS way

what is the diffrences? which is better to use (except the SDL is less writing)?

shamgar
  • 120
  • 9

1 Answers1

0
  • SDL => Full-schema (or schema-first).
  • JavaScript => Object-based (or code first).

for the post below.

"Completely relying on the full-schema text to create a GraphQL schema has a few drawbacks. You’ll need to put in some effort to make the code modularized and clear, and you’ll have to rely on coding patterns and tools to keep the schema-language text consistent with the tree of resolvers (aka resolvers map). These are solvable problems.

The biggest issue I see with the full-schema method is that you lose some flexibility in your code. All your types have to be written in that specific way that relies on the schema-language text. You can’t use constructors to create some types when you need to. You’re locked into this string-based approach. Although the schema-language text makes your types more readable, in many cases, you’ll need flexibility over readability.

The object-based method is flexible and easier to extend and manage. It does not suffer from any of the problems I just mentioned. Your code will be modular with it because your schema will be a bunch of objects. You also don’t need to merge modules, because these objects are designed and expected to work like a tree.

The only issue I see with the object-based method is that you have to deal with a lot more code around what’s important to manage in your modules (types and resolvers). Many developers see that as noise, and I do not blame them.

If you’re creating a small, well-defined GraphQL service, using the full-schema-string method is probably okay. However, in bigger and more agile projects, I think the more flexible and powerful object-based method is the way to go."

Source

sha'an
  • 1,032
  • 1
  • 11
  • 24