Graphql-js: Mutate nested field?

Created on 14 Apr 2016  路  1Comment  路  Source: graphql/graphql-js

I'm trying to grasp how nested fields work..would appreciate any pointers. What I'm trying to do is have a generic object type using the scalar type. Here's what I jacked from stackoverflow and made into my GenericObjectType:

export const GraphQLGenericObjectType = new GraphQLScalarType({
  name: 'GenericObjectType',
  serialize: value => {
    return value
  },
  parseValue: value => {
    return value
  },
  parseLiteral: ast => {
    if(ast.kind !== Kind.OBJECT) {
      throw new GraphQLError("Query error: Can only parse object but got a: " + ast.kind, [ast])
    }
    return ast.value
  }
})

Here's the schema I'm using it on:

export const RootBucket = new GraphQLObjectType({
  name: 'Bucket',
  description: 'A Bucket',
  fields: () => ({
    data: {type: GraphQLGenericObjectType},
    desc: {type: new GraphQLNonNull(GraphQLString)},
    id: {type: new GraphQLNonNull(GraphQLString), description: 'The id'},
    createdAt: {type: GraphQLString, description: 'The datetime the bucket was created'},
    updatedAt: {type: GraphQLString, description: 'The datetime the bucket was last updated'},
    title: {type: new GraphQLNonNull(GraphQLString)}
  })
})

What's the correct way to write a query to add a "bucket" to the database, with the data property some generic object such as data = {foo: 'bar'}

I've tried this:

mutation {
  createBucket(desc: "", title: "Test 5", data: {foo: "bar"}) {
    id
  }
}

But that results in the following error:

res { errors: 
 [ [Error: Argument "data" has invalid value {foo: "bar"}.
 Expected type "GenericObjectType", found {foo: "bar"}.] ] }

I assume I have missed the point/purpose of something, but I'm not even sure where to begin?

Most helpful comment

Sorry for the delay.

Unfortunately right now custom scalars do not support object literals in the language.

The issue isn't anything you've done, it's just that the validator sees that you're using a "scalar" for the "data" field, but have provided an object (not a scalar) and thus is pointing out what it thinks is an issue.

Other ways I've seen this pattern done before is instead providing this as a JSON string:

mutation {
  createBucket(desc: "", title: "Test 5", data: "{foo: \"bar\"}") {
    id
  }
}

And running JSON.parse() on the data argument. That allows for truly arbitrary unstructured properties.

>All comments

Sorry for the delay.

Unfortunately right now custom scalars do not support object literals in the language.

The issue isn't anything you've done, it's just that the validator sees that you're using a "scalar" for the "data" field, but have provided an object (not a scalar) and thus is pointing out what it thinks is an issue.

Other ways I've seen this pattern done before is instead providing this as a JSON string:

mutation {
  createBucket(desc: "", title: "Test 5", data: "{foo: \"bar\"}") {
    id
  }
}

And running JSON.parse() on the data argument. That allows for truly arbitrary unstructured properties.

Was this page helpful?
0 / 5 - 0 ratings