1

I am making the following query in GraphQL:

{
  metal(silver_bid_usd_toz: 1) {
    silver_bid_usd_toz
  }
}

which returns

{
  "data": {
    "metal": {
      "silver_bid_usd_toz": 16.45
    }
  }
}

The JSON object returned by the API is flat:

{
  silver_bid_usd_toz: 123,
  gold_bid_usd_toz: 123,
  copper_bid_usd_toz: 123
}

I don't understand what the int 1 in my graphql query means metal(silver_bid_usd_toz: 1)

It doesn't matter what I change it to, it could be 1 or 355, but it is required for the query to work. Why cant I just do

{
  metal(silver_bid_usd_toz) {
    silver_bid_usd_toz
  }
}

My schema looks like this:

 module.exports = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    description: '...',
    fields: () => ({
      metal: {
        type: MetalType,
        args: {
          gold_bid_usd_toz: { type: GraphQLFloat },
          silver_bid_usd_toz: { type: GraphQLFloat }
        },
        resolve: (root, args) => fetch(
          `api_url`
        )
        .then(response => response.json())
      }
    })
  })
});
reknirt
  • 2,237
  • 5
  • 29
  • 48

1 Answers1

1

You are passing silver_bid_usd_toz as an argument for the field, but apparently you are not using it in the resolve function, so it's being ignored.

It seems to be the reason why the result is always the same when you change the argument value.

But it is weird when you say that it is required for the query to work, since it is not defined as a GraphQLNonNull type. It should be possible to query this field without passing any argument, according to the Schema you passed us.

luislhl
  • 1,136
  • 1
  • 10
  • 23
  • Yeah you're right. It was failing because I wrapped it in parentheses, this works as expected: metal { silver_bid_usd_toz } – reknirt Jun 01 '18 at 02:26
  • Oh, that makes sense. By just looking at it I didn't notice the error. Good to know you spotted the problem. ;) – luislhl Jun 01 '18 at 02:28