0

I'm new to graphql and am trying to set up a field for one of my users attributes which is more than just a String or Int. It should be a reference to an object which with my understanding should be a type or input

Here's my schema:

schema.graphql

type Mutation {
  addOrChangeUserDetails(location: String, coordinates: LocationCoordinateInput): LocationCoordinates
}

type LocationCoordinates {
  lat: Int!
  long: Int!
}

input LocationCoordinateInput {
  coordinates: LocationCoordinates
}

And here is my query

index.js

import gql from 'graphql-tag';

const ADD_OR_CHANGE_USER_DETAILS_MUTATION = gql`
  mutation ADD_OR_CHANGE_USER_DETAILS_MUTATION(
    $location: String,
    $coordinates: LocationCoordinates
  ) {
    addOrChangeUserDetails(
      location: $location,
      coordinates: $coordinates
    ) {
      location
      coordinates
    }
  }
`;

The error i'm getting is:

Error: The type of LocationCoordinateInput.coordinates must be Input Type but got: LocationCoordinates.

I originally didn't have a separate entry for the LocationCoordinateInput and thought I could just use the type LocationCoordinates as my addOrChangeUserDetails argument but after looking into the issue it seems if I want to refer to something that will be more than just a String or Int etc then I need to create a custom input.

Is this the correct approach for creating a "richer" field and if so why might my code not be working?

red house 87
  • 1,837
  • 9
  • 50
  • 99
  • As the error indicates, only input types can be used for arguments, not object types. This question comes up a lot. Please see [here](https://stackoverflow.com/questions/54119116/array-of-objects-apollo-server-and-post-from-react?noredirect=1#comment95072872_54119116), [here](https://stackoverflow.com/questions/52744900/apollo-graphql-type-must-be-input-type), and [here](https://stackoverflow.com/questions/46158288/graphql-how-to-reuse-same-type-for-query-and-mutation/46159440). – Daniel Rearden Jan 16 '19 at 16:08
  • Thanks for your suggestions, they got me fairly far. The only bit i'm confused about now is the type of data i'm saying `lng` and `lat` is. If I pass whole numbers then it works fine and accepts that they are of type `Int` but if I add decimal places then it says they are not `Int`. What type should I be using for decimals? – red house 87 Jan 16 '19 at 16:33
  • Bit confusing as typeof(3.22) will evaluate to a number in javascript – red house 87 Jan 16 '19 at 16:34
  • 1
    Use a `Float` scalar instead. Integers can be coerced into floats but not vice versa in GraphQL. From the spec: "GraphQL servers may coerce non‐integer internal values to integers when reasonable without losing information, otherwise they must raise a field error. Examples of this may include returning 1 for the floating‐point number 1.0, or returning 123 for the string "123". In scenarios where coercion may lose data, raising a field error is more appropriate. For example, a floating‐point number 1.2 should raise a field error instead of being truncated to 1." – Daniel Rearden Jan 16 '19 at 17:01
  • Thanks, all clear! I also amended my code like so: https://jsfiddle.net/2e3576ao/ . Can I just double check this is the right approach? – red house 87 Jan 16 '19 at 17:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186803/discussion-between-daniel-rearden-and-red-house-87). – Daniel Rearden Jan 16 '19 at 17:31

0 Answers0