Graphql-tools: Is this possible for types to accept arguments?

Created on 20 May 2016  Â·  9Comments  Â·  Source: ardatan/graphql-tools

  schema {
    query: RootQuery
  }

  type RootQuery {
    organization(id: Int, name: String): Organization
    organizations(limit: Int, skip: Int): [Organization]
    project(id: Int, name: String): Project
    projects(limit: Int, skip: Int): [Project]
  }

  ...

  type Project(myParameter:String) {          // <-------- Doesn't work
    id: Int!
    name: String!
    description: String
    organization: Organization
    tasks: [Task]
  }

Is there a way to add the functionality shown in the example above? I've tried the above approach, but it gives me a syntax error Expected {, found (

Most helpful comment

Yeah, the parameter goes on the field itself, like so:

type Project {
    id: Int!
    name: String!
    description: String
    organization: Organization
    tasks(status: String): [Task]
  }

All 9 comments

What would the parameter do?

Get passed to the resolver. It could be pagination variables, a max results limit, anything, as long it got passed to the resolver.

I take it I'm going about it wrong.

I'd like to be able to write a query like this

{
  organizations(skip:5) {
    id
    name
    projects(limit:10) {
      id
      name
      tasks(status:"active"){
        id
        name
      }
    }
  }
}

Yeah, the parameter goes on the field itself, like so:

type Project {
    id: Int!
    name: String!
    description: String
    organization: Organization
    tasks(status: String): [Task]
  }

@jakobrosenberg Yeah, arguments go only on fields. If you have an argument that's shared between all fields that return a certain type, you unfortunately have to repeat that every time (or add another type in-between, as a level of indirection, and add the argument there). That's sort of what Relay does for pagination.

The query you wrote above should already work. All you have to do is add the status argument to tasks.

Thanks guys, this was doing my head in. I've been looking for documentation for the shorthand schema, but with no luck.

@jakobrosenberg In case you didn't already find it, this cheat sheet is pretty useful.

I came across it last night, but I never realized the pattern between the root query and the other types. It's so obvious now.

Sorry for this. But. Do you have an example in apollo-server schema??

{
  organizations(skip:5) {
    id
    name
    projects(limit:10) {
      id
      name
      tasks(status:"active"){
        id
        name
      }
    }
  }
}

How handle with tasks into projects in the resolver?

@hmontes I hope you don't need this anymore, but found this article that help me set it up for my Apollo implemetation: https://spin.atomicobject.com/2017/03/30/graphql-apollo-building-server/

Example from article:

const schema = 
type Person {
  name: String!
  age: Int
  gender: Gender
  height(unit: HeightUnit = METER) : Float
}

const personResolver = {
  Person: {
    name: ({ name }) => name.toUpperCase(),
    height: ({ height }, { unit }) => unit === 'METER' ? height * 0.0254 : height
  },
};
Was this page helpful?
0 / 5 - 0 ratings