electo
electo

Reputation: 128

Graphql: Can't get args passed in extended type

So i'm kinda new to graphql and i'm facing a problem which i don't know how to resolve. I'm using nodeJs and apollo.

I have a profile micro-service who has an User type:

type User @key(fields: "_id") {
  ${userFields}
}

And i also have a progression micro-service where i extend the User type:

extend type User @key(fields: "_id") {
  _id: ID! @external,
  progression: Progression
}

type Progression {
  currentGOTW: Int,
  totalGOTW: Int,
  video(playlistId: String!, itemIndex: Int!): [Int]
}

As you can see i extended the User type to add a Progression field, and the problem comes from the video field which needs two arguments.

The progression resolver looks like this:

User: {
  async progression (user, data) {
    console.log(data)
    const videoProgression = await UserWatched.get({ playlist_id: data.playlistId, item_index: data.itemIndex, userRef: user._id })
    return videoProgression
  }
}

In this resolver i'm expecting to get playlistId and itemIndex in data but when i console.log it, data is empty. I don't have any errors and i tried displaying args that are after data in the resolver function but they didn't contain what i want either.

This is my query:

const query = gql`
{
  User {
    progression {
      video(playlistId: "playlist", itemIndex: 3)
    }
  }
}`

And i need to get the "playlist" and the 3 in my resolver.

I didn't find any answers for that and i'm starting to think that maybe it's impossible for now...

Upvotes: 2

Views: 379

Answers (1)

Nicholas Harder
Nicholas Harder

Reputation: 1558

First off, I'm not sure if your initial query is correct (maybe some code is just missing from these examples). You're trying to query for a User with this,

{
  User {
    ...
  }
}

but you must define a "root query" for this step. There is a Query (and Mutation) type in every schema, that is where you place root level queries. Here is an example of what you want,

...
type Query {
  user(id: ID!): User!
}

then similarly, you would define a resolver to retrieve this User by id. It might look like this,

Query: {
  user(source, args, context) {
    return context.dataSources.getUserById(args.id)
  }
}

So now you can make a root level query for your user. The second problem is that you're trying to access the arguments one resolver too low in execution. You are trying to use the arguments passed to Progression.video resolver in the User.progression resolver. Instead, you need to modify the schema so that User.progression resolver has those arguments.

extend type User @key(fields: "_id") {
  _id: ID! @external,
  progression(playlistId: String!, itemIndex: Int!): Progression
}

Your final query should be similar to this,

const query = gql`
  query GetUser($id: ID!, $playlistId: String!, $itemIndex: Int!) {
    user(id: $id) {
      progression(playlistId: $playlistId, itemIndex: $itemIndex) {
        video
      }
    }
  }`

This is definitely not impossible! You'll just have to get used to GraphQL. Good Luck!

Upvotes: 1

Related Questions