In graphql what is difference between a `type query` and `extend type query`?

66 views Asked by At

In graphql what is difference between a type query and extend type query?

Ex: difference between

type Query {
    product(id: String!): Product
}

and

extend type Query {
    DeviceDetail(devId: String!): DeviceDetail
}

Will appreciate if you could add an example for your explanation.

1

There are 1 answers

0
Brian Clozel On BEST ANSWER

Object extension is supported by the GraphQL spec as a way to add new properties to an existing type.

In this case, you will find extend type Query in some schemas as a way to "modularize" a schema, for example:

in schema.graphqls:

type Query {
  greeting: String
}

in book.graphqls:

extend type Query {
  bookById(id: ID!): Book
}

type Book {
  id: ID!
  title: String!
  author: Author
}

type Author {
  name: String
}

Without this, you would need to either declare all queries in the same schema file, or you would get an error when GraphQL parses the schema files as the Query type is defined several times.

Other apps use "namespacing" as a way to better organize the schema and avoid flattening all queries under the same type.