Skip to content

Add field directives on Info #1789

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

Open
estrada9166 opened this issue Mar 13, 2019 · 3 comments
Open

Add field directives on Info #1789

estrada9166 opened this issue Mar 13, 2019 · 3 comments

Comments

@estrada9166
Copy link

estrada9166 commented Mar 13, 2019

It's possible to add a new field on info with the directives of that field?

Reason:

The main reason for this is that using a fieldResolver it'll be possible to add some extra logic on selected fields.

Example:

schema

directive @uppercase on FIELD_DEFINITION

type Me {
  email: String!
  username: String!
  fullName: String! @uppercase
  apiKey: String!
}

type Query {
  getMe: Me!
}

fieldResolver

function fieldResolver(value, args, ctx, info) {
  const result = defaultFieldResolver(value, args, ctx, info)

  const uppercase = info.fieldDirectives.find(directive => directive.name.value === 'uppercase') 

  if (uppercase) {
    return result.toUpperCase()
  }

  return result
}

using express-graphql

app.use('/graphql', (req, res) => {
  graphqlHTTP({
    schema,
    rootValue,
    graphiql: true,
    fieldResolver
  })(req, res)
})

Possible solution

In this method, we can get the directives from fieldDef.astNode.directives

export function buildResolveInfo(
exeContext: ExecutionContext,
fieldDef: GraphQLField<*, *>,
fieldNodes: $ReadOnlyArray<FieldNode>,
parentType: GraphQLObjectType,
path: ResponsePath,
): GraphQLResolveInfo {
// The resolve function's optional fourth argument is a collection of
// information about the current execution state.
return {
fieldName: fieldDef.name,
fieldNodes,
returnType: fieldDef.type,
parentType,
path,
schema: exeContext.schema,
fragments: exeContext.fragments,
rootValue: exeContext.rootValue,
operation: exeContext.operation,
variableValues: exeContext.variableValues,
};
}


Also, if this is accepted, I can work on that

@mjmahone
Copy link
Contributor

We probably want a more comprehensive solution to getting a directive on various schema definitions.

I think the core idea here is right, but we'd also like to make it easy to access directives for things like type definitions with directives on them.

I think this is possible to do already by grabbing the astNode on the FieldDefinition, but I agree we could use some sort of easier access to the directives. Especially when you throw schema extensions and pure-JS-class created schemas into the mix.

@yaacovCR
Copy link
Contributor

Fwiw graphql-tools has a getDirective method and cousins that might be helpful https://www.graphql-tools.com/docs/api/modules/utils_src#getdirective

@yukukotani
Copy link

Here is my dirty workaround:

import { getDirectives } from '@graphql-tools/utils';

function getDirectivesFromInfo(info: GraphQLResolveInfo) {
  const field = info.parentType.getFields()[info.fieldName];
  return getDirectives(info.schema, field);
}

It's better if GraphQLResolveInfo contains fieldDef so that we can just pass it to getDirectives function.

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

No branches or pull requests

5 participants