0

So I'm trying to figure out how to return an array of strings in GraphQL. Basically I have a to-do list with dependencies, so for each item there's a list of things that other item relies on by ID. I'm a little new to all this, but I've done a lot of searching on the GraphQL docs and on here and haven't found anything yet, so apologies in advance if this is a dumb question. I'm using lodash in the resolve function.

// dummy data
var todos = [
  { name: 'Repair Driveway', dependencies: ['2' ,'3'], id: '1' },
  { name: 'Research driveway repair', dependencies: [], id: '2' },
  { name: 'Buy driveway repair tools', dependencies: ['2'], id: '3' },
  { name: 'Get groceries', dependencies: [], id: '4'}
];

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    deadline: { type: GraphQLDateTime },
    dependencies: { type:   } // This is where I'm not sure what to do.
  })
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    todo: {
      type: TodoType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args){
        console.log("Queried:" );
        return _.find(todos, { id: args.id });
      }
    }
  }
})

2 Answers 2

3

GraphQL provides a class that makes a lists of a given object type new GraphQLList(objectType).

You can use it like:

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    deadline: { type: GraphQLDateTime },
    dependencies: { type: new GraphQLList(GraphQLString)  }
  })
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

const TodoType = new GraphQLObjectType({
    name: 'Todo',
    fields: () => ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        deadline: { type: GraphQLDateTime },
        dependencies: { type: [GraphQLString!] } // This is where I'm not sure what to do.
    })
});

Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.