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

Allow to override default type resolver #1332

Merged
merged 2 commits into from
Jan 20, 2019

Conversation

IvanGoncharov
Copy link
Member

Having global default typeResolver is very useful for writing GraphQL proxies and for mocking missing types. For example it would be very easy to mock types with missing resolver like this:

import {
  defaultFieldResolver,
  defaultTypeResolver,
} from 'graphql';

function mockFieldResolver(value, args, ctx, info) {
  const default = defaultFieldResolver(value, args, ctx, info);
  return default === undefined ? mockType(info.returnType) : default;
}

function mockType(type) {
  //...
  if (type === GraphQLString) {
    return 'mock string';
  }
}

function mockTypeResolver(value, ctx, info) {
  const defaultType = defaultTypeResolver(value, ctx, info);
  if (defaultType === undefined) {
    const types = info.schema.getPossibleTypes(info.valueType);
    const random = Math.floor(Math.random() * (types.length - 1))
    return types[random];
  }
  return defaultType;
}

const result = graphql({
  schema, 
  source,
  fieldResolver: mockFieldResolver,
  typeResolver: mockTypeResolver,
});

Copy link
Contributor

@freiksenet freiksenet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I guess @leebyron should approve.

const resolveTypeFn = returnType.resolveType || exeContext.typeResolver;
const contextValue = exeContext.contextValue;
const typeInfo = { ...info, valueType: returnType, valuePath: path };
const runtimeType = resolveTypeFn(result, contextValue, typeInfo);
Copy link
Contributor

@leebyron leebyron Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid creating a new object for every field resolution?

Perhaps these lines should be:

const runtimeType = returnType.resolveType
  ? returnType.resolveType(result, exeContext.contextValue, info)
  : exeContext.typeResolver
    ? exeContext.typeResolver(returnType, result, exeContext.contextValue, info)
    : defaultResolveTypeFn(returnType, result, exeContext.contextValue, info)

Also, path is already in info, so I don't think we need to add it again as valuePath?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to this type signature - I'm not sure how valuable returnType will be for a default resolver - if it deserves to be the first argument or not. I imagine result would still be the most useful.

Another option might be to just provide returnType as another argument after info to all type resolvers - not just default ones?

@IvanGoncharov
Copy link
Member Author

Another option might be to just provide returnType as another argument after info to all type resolvers - not just default ones?

@leebyron Done 👍
Can you please review one more time?

@IvanGoncharov IvanGoncharov added this to the v14.1.0 milestone Oct 4, 2018
@IvanGoncharov IvanGoncharov modified the milestones: v14.1.0, v14.2.0 Jan 16, 2019
@IvanGoncharov IvanGoncharov added the PR: feature 🚀 requires increase of "minor" version number label Jan 17, 2019
@IvanGoncharov
Copy link
Member Author

@mjmahone @Neitsch Can you please review this PR?

Copy link

@Neitsch Neitsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure that I get this change correctly: You allow to specify a custom type resolved, that if specified replaces the default resolver. Does that sound right?

Overall I am not a fan that we need to change types in so many places for this to work, but improving that is outside of the scope of this PR :)

});

function typeResolver(source, context, info, abstractType) {
return schema.getPossibleTypes(abstractType)[0];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this line to something like return schema.getPossibleTypes(abstractType).find(graphQLType => graphQLType.name === "FooObject");. Makes it clearer what we are returning.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neitsch Great suggestion 👍
One thing is that it's enough to return string with type name from typeResolve so matching type by name looks unnecessary if you can just return name.

So I just change it to be more explicit.

@IvanGoncharov
Copy link
Member Author

Just to make sure that I get this change correctly: You allow to specify a custom type resolved, that if specified replaces the default resolver. Does that sound right?

@Neitsch Yes, exactly 👍

@IvanGoncharov IvanGoncharov changed the title Allow providing custom default type resolver Allow to provide custom default type resolver Jan 20, 2019
@IvanGoncharov IvanGoncharov changed the title Allow to provide custom default type resolver Allow to override default type resolver Jan 20, 2019
@IvanGoncharov IvanGoncharov merged commit fd308ce into graphql:master Jan 20, 2019
@IvanGoncharov IvanGoncharov deleted the resolveType branch January 20, 2019 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed PR: feature 🚀 requires increase of "minor" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants