简体   繁体   中英

Best way to represent GraphQL schema

I start use graphQl query language and i use GraphQL Schema Definition Language as strings types to define types,but i see other developers use GraphQLObjectType function, that the JavaScript reference implementation of GraphQL uses to store information about the schema,and i confused about the best way to define the schema,i need know in what case we can use GraphQLObjectType

const queryType = new GraphQLObjectType({
  name: "Query",
  fields: {
    posts: {
      type: postType
    },
    author: {
      name: "author",
      type: authorType,
      arguments: { id: { type: new GraphQLNonNull(GraphQLInt) } }
    }
  }
});

or GraphQL Schema Definition Language

type Author {
  id: Int!
  firstName: String
  lastName: String
  posts: [Post]
}type Post {
  id: Int!
  title: String
  author: Author
  votes: Int
}type Query {
  posts: [Post]
  author(id: Int!): Author
}

"Best way" will depend quite a bit on your circumstances. However, I think using schema definitions provides benefits worth considering.

  • It's language agnostic. If you need to share this schema with anyone else, or use it in other tools or components, it will be much easier to do so using SDL.
  • It's easier to compose chunks together. You can define interfaces and types in separate files and pull them together as you see fit.
  • It's more concise. In my opinion, this makes it easier to understand when you come back to it 6 months later. I also think this makes it easier to extend and modify.

For these reasons, I believe SDL is the best way. BUT, in JavaScript, both ways work, so if you're more comfortable writing the code and the above considerations don't matter to you, stick with what works for you!

EDIT: sorry, I was a bit too hasty in answering.

So indeed, when it comes to defining your schema, you can either do this:

var schema = buildSchema(`
  type User {
    id: String
    name: String
  }
 
  type Query {
    user(id: String): User
  }
`);

or build your schema programmatically by using GraphQLObjectType, etc. It depends on your use case but if my schema gets lengthy in size, I'd also like to have to manage each object separately. More generally, this is what graphql says about it:

This is particularly useful if you want to create a GraphQL schema automatically from something else, like a database schema. You might have a common format for something like creating and updating database records. This is also useful for implementing features like union types which don't map cleanly to ES6 classes and schema language.


Some clarification here: It's not either one or the other. The GraphQL Schema Definition Language is used to talk about GraphQL schemas in a language-agnostic way . You can't use it as-is, it's just for describing your schema. Since, you're using Javascript which I assume, you must use what the library for that language provides to build your schema.

What is the GraphQLObjectType?

To quote the docs :

The most basic components of a GraphQL schema are object types, which just represent a kind of object you can fetch from your service, and what fields it has. ... it's a type with some fields. Most of the types in your schema will be object types.

So this is as the name suggests, what you use to describe an object. GraphQLString to describe string fields, etc.

GraphQL services can be written in any language. Since we can't rely on a specific programming language syntax, like JavaScript, to talk about GraphQL schemas, we'll define our own simple language. We'll use the "GraphQL schema language" - it's similar to the query language, and allows us to talk about GraphQL schemas in a language-agnostic way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM