How to deprecate fields in GraphQL

Kevin Simper
2 min readJul 2, 2017

--

You can very easily deprecate a field and if you are using GraphiQL it will show you right in the interface.

There is no guide on the graphql about what you do with migrations and we are having a point now where we need to change some of the fields that we have in our GraphQL api.

So a little bit of digging and I found out you just do it by adding it like this:

var UserType = new GraphQLObjectType({
name: 'User',
fields: {
name: {
type: GraphQLString,
deprecationReason: 'We split up the name into two'
},
firstname: { type: GraphQLString },
lastname: { type: GraphQLString }
}
})

Now if point a GraphiQL browser towards your api, you will be able to see this in the documentation sidebar.

When you add a deprecationReason it will automatically marked as

isDeprecated: true

as you can see here in the request that GraphiQL makes:

You can read more about the api for declaring these types. http://graphql.org/graphql-js/type/#graphqlobjecttype

Also the work that has been done with GraphiQL is super amazing, you can read more about that here.
https://github.com/graphql/graphiql

--

--

Kevin Simper
Kevin Simper

Written by Kevin Simper

I really like building stuff with React.js and Docker and also Meetups ❤

Responses (1)