Skip to content

Resolver args type not correctly propagated from GraphQLObjectType #2481

Closed
@harrysolovay

Description

@harrysolovay

I'm running into issues relating to the inference of arguments and return types from within a given GraphQLObjectType.

My query interface looks like this:

interface Query {
  tweets: Tweet[];
  user: User;
}

And my schema like this:

export const schema = new GraphQLSchema({
  query: new GraphQLObjectType<Context, Query>({
    fields: {
      tweets: {
        resolve: async (_0, args) => {
          // `tweets` resolution here
        },
        type: new GraphQLList(TweetType),
      },
      user: {
        args: {
          id: {type: GraphQLID},
        },
        resolve: async (_0, args) => {
          // `user` resolution here
        },
        type: UserType,
      },
    },
    name: "Query",
  }),
});

The tweets query is meant to accept no arguments, whereas the user query accepts the user's id. So I specify the 3rd generic argument, which I hoped would map to the correct fields:

export const schema = new GraphQLSchema({
- query: new GraphQLObjectType<Context, Query>({
+ query: new GraphQLObjectType<Context, Query, {tweets: undefined; user: {id: string}}>({
    fields: {
...

Although I expect for those types to flow through to their respective resolver arguments, they do not. Instead, the entire map flows through:

Screen Shot 2020-03-08 at 1 00 35 PM

While I could provide a union of the two types, this lacks specificity, and I was wondering if there's a better way?

Additionally, (perhaps this merits another issue), the resolver return types aren't inferred either (I have to manually append the return signature).

-         resolve: async (test, args) => {
+         resolve: async (test, args): Promise<Tweet[]> => {
          try {
            const tweets = await firestore.collection("tweets").get();
            return tweets.docs.map((tweet) => ({id: tweet.id, ...tweet.data()})) as Tweet[];
          } catch (error) {
            throw new ApolloError(error);
          }
        },
        type: new GraphQLList(TweetType),
      },
      user: {
        args: {
          id: {type: GraphQLID},
        },
-       resolve: async (_0, args) => {
+       resolve: async (_0, args): Promise<User> => {

Any help or advice would be greatly appreciated! Thank you!

Kind regards,

Harry

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions