Graphql-js: Values are converted from/to arrays

Created on 8 May 2017  Â·  2Comments  Â·  Source: graphql/graphql-js

trying out with the following program:

const { buildSchema, graphql } = require('graphql');

graphql(
  // schema
  buildSchema(`
    type RootQuery {
      x(y: [String]): String
    }

    schema {
      query: RootQuery
    }
  `),
  // query
  `
    query get ($var: [String]) {
      x(y: $var)
    }
  `,
  // root
  {
    x (query) {
      console.log(query);
      return query.y;
    }
  },
  // context
  null,
  // variables
  {
    var: ''
  })
  .then(({ data }) => console.log(data), console.error);

the output of this is:

{ y: [ '' ] }
{ x: '' }

so, I provide the wrong type in query variables and instead of an error/warning it gets wrapped in an array and passed to the resolver. Likewise, even though I return an array instead of a string from the resolver, the query result is a string. I'm not sure whether this is by design but to me this just seems like a potential source for bugs.

Most helpful comment

The String → [String] transform on the way in (y) is part of the spec: see List’s Input Coercion.

If the value passed as an input to a list type is not a list and not the null value, it should be coerced as though the input was a list of size one, where the value passed is the only item in the list. This is to allow inputs that accept a “var args” to declare their input type as a list; if only one argument is passed (a common case), the client can just pass that value rather than constructing the list.

This ends up coming in handy for consumers of your GraphQL API. For example, releaseGroups(type: ALBUM) is nicer than remembering to have to type releaseGroups(type: [ALBUM]), and still allows for releaseGroups(type: [ALBUM, EP]).

The [String] → String conversion on the way out (x) is likely just a side effect of JavaScript’s Array.protoype.toString being, effectively, arr.join(','). I don't think GraphQL is doing anything special to support that. If you returned ['a', 'b'] I'm guessing x would be 'a,b'.

All 2 comments

The String → [String] transform on the way in (y) is part of the spec: see List’s Input Coercion.

If the value passed as an input to a list type is not a list and not the null value, it should be coerced as though the input was a list of size one, where the value passed is the only item in the list. This is to allow inputs that accept a “var args” to declare their input type as a list; if only one argument is passed (a common case), the client can just pass that value rather than constructing the list.

This ends up coming in handy for consumers of your GraphQL API. For example, releaseGroups(type: ALBUM) is nicer than remembering to have to type releaseGroups(type: [ALBUM]), and still allows for releaseGroups(type: [ALBUM, EP]).

The [String] → String conversion on the way out (x) is likely just a side effect of JavaScript’s Array.protoype.toString being, effectively, arr.join(','). I don't think GraphQL is doing anything special to support that. If you returned ['a', 'b'] I'm guessing x would be 'a,b'.

Thanks for the great answer, @exogen

Was this page helpful?
0 / 5 - 0 ratings

Related issues

henry74 picture henry74  Â·  4Comments

matthewgertner picture matthewgertner  Â·  4Comments

davide-ganito picture davide-ganito  Â·  4Comments

closertb picture closertb  Â·  3Comments

galki picture galki  Â·  3Comments