Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static values in GraphQL

Tags:

graphql

Is there a way to produce static values in a graphql query?

For example, let's say that I have a user object with a name and email field. For some reason, I always want the status of a user to be "ACCEPTED". How can I write a query that accomplishes this?

What I want to do:

query {
  user(id: 1) {
    email
    name
    status: "ACCEPTED"
  }
}

The result I want:

{
  "data": {
    "user": {
      "email": "[email protected]",
      "name": "me",
      "status": "ACCEPTED"
    }
  }
}
like image 409
Mark Karavan Avatar asked Oct 30 '22 00:10

Mark Karavan


1 Answers

You can make your resolve function return a static value, e.g. like this in JavaScript:

const HomeWorldType = new GraphQLObjectType({
  name: 'HomeWorld',
  fields: () => {
    return {
      id: {
        type: GraphQLInt,
        resolve: () => 7,
      },
      name: { type: GraphQLString },
      climate: { type: GraphQLString },
      population: { type: GraphQLString },
    }
  }
})
like image 53
Tyler Collier Avatar answered Jan 02 '23 20:01

Tyler Collier