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

Resolver args type not correctly propagated from GraphQLObjectType #2481

Closed
harrysolovay opened this issue Mar 8, 2020 · 1 comment
Closed
Labels

Comments

@harrysolovay
Copy link

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

@harrysolovay harrysolovay changed the title Resolver args type not correctly propogated from GraphQLObjectType Resolver args type not correctly propagated from GraphQLObjectType Mar 10, 2020
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this issue Mar 12, 2020
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this issue Mar 12, 2020
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this issue Mar 12, 2020
IvanGoncharov added a commit that referenced this issue Mar 13, 2020
@IvanGoncharov
Copy link
Member

@harrysolovay Thanks for reporting 👍
Totally agree that 3rd-argument is very confusing so I removed TArgs in #2488.
It was added by DefinetlyTyped maintainers and we inherited when we adopted TS typings from them.

As for the long term solution, there is a discussion about adding more complex TS typings in #2188
Also if you goal to have maximum type safety in TS I would suggest using
https://github.com/MichalLytek/type-graphql or https://github.com/graphql-nexus/schema

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

2 participants