Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subselection not recognized #49

Closed
c-riq opened this issue Jan 4, 2018 · 2 comments
Closed

Subselection not recognized #49

c-riq opened this issue Jan 4, 2018 · 2 comments
Labels

Comments

@c-riq
Copy link
Contributor

c-riq commented Jan 4, 2018

I added this property to Home in https://github.com/graphcool/graphcool-server-example/blob/master/src/resolvers/Home.ts

perNight: {
    resolve: async ({ id }, args, ctx: Context, info) => {
      const place = await ctx.db.query.place({ where: { id } },"{ pricing { perNight } }")
      return place.pricing.perNight
    },
},

And perNight: Int! to the Home type in schema.graphql

Then running this query in the playground

{
  topHomes {
    perNight
  }
}

returns the error

"Field 'places' of type 'Place' must have a sub selection. (line 2, column 3):\n  places(where: $_where, orderBy: $_orderBy, skip: $_skip, after: $_after, before: $_before, first: $_first, last: $_last)\n  ^"

but this query works as expected

{
  topHomes {
    perNight
    id
  }
}
@marktani
Copy link
Contributor

marktani commented Jan 4, 2018

Thanks a lot for bringing this up! The reason is that the resolver itself assumes the existence of an id (you can see this in the first argument which is { id }). So, if there is no id after all, an error is returned (the error message is not too helpful though).

You can solve this use case by providing a fragment option to the resolver object, which ensures that id is part of the query.

This same situation is already handled in the numRatings field of the Home type:

  numRatings: {
    fragment: `fragment NumRatings on Place { id }`,
    resolve: async ({ id }, args, ctx: Context, info) => {
      const places = await ctx.db.query.places({ where: { id } })
      return places.length
    },
  },

So applying that same logic to perNight leaves us with this:

perNight: {
    fragment: `fragment pricing on Place { id }`,
    resolve: async ({ id }, args, ctx: Context, info) => {
      const place = await ctx.db.query.place({ where: { id } },"{ pricing { perNight } }")
      return place.pricing.perNight
    },
},

This is a great scenario that I should cover here: graphql-boilerplates/node-graphql-server#35

@heymartinadams
Copy link

Yes, @marktani, a more specific error message would be helpful 🙌🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants