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

Support for operation directives #947

Closed
Fryuni opened this issue Jul 16, 2021 · 2 comments · Fixed by #952
Closed

Support for operation directives #947

Fryuni opened this issue Jul 16, 2021 · 2 comments · Fixed by #952
Labels
type/feat Add a new capability or enhance an existing one

Comments

@Fryuni
Copy link

Fryuni commented Jul 16, 2021

The discussion on issue #53 seems to be about schema directives, so I'm opening a new issue regarding query directives which is also not supported,

It seems quite simple to add this feature by passing an extra field to the GraphQLSchema constructor here:

nexus/src/builder.ts

Lines 1700 to 1709 in 8e0ce1b

const schema = new GraphQLSchema({
query: Query,
mutation: Mutation,
subscription: Subscription,
types: objValues(typeMap),
extensions: {
...config.extensions,
nexus: schemaExtension,
},
}) as NexusGraphQLSchema

Since that is just one function down from makeSchema and uses the same type, all it requires is adding a new field on the SchemaConfig here and passing it along. I tested it locally, and it works perfectly. The schema gets generated correctly with the directive specification at the top, and the information is passed to the resolvers at info.fieldNodes.directives as graphql-js defines.

Here is how it ended up being with this change:

/* file: directives.ts */
import {GraphQLDirective, GraphQLInt} from 'graphql';

export const cacheDirective = new GraphQLDirective({
    name: 'cached',
    args: {
        lifetime: {
            type: GraphQLInt,
        },
    },
    locations: ['FIELD'],
});

/* file: schema.ts */
import {makeSchema} from 'nexus';
import {cacheDirective} from './directives';

export const NexusGraphQLSchema = makeSchema({
    directives: [cacheDirective],
    ...
});

/* file: testResolver.ts */
import {queryField} from 'nexus';
import {cacheDirective} from '../directives';
import {getArgumentValues} from 'graphql/execution/values';

export const testField = queryField(def => def.string('testField', {
     resolve: (_, __, ___, info) => {
        const cacheConfiguration = getDirectiveValues(cacheDirective, info.fieldNodes[0], info.variableValues);
        return cacheConfiguration?.lifetime?.toFixed(0) ?? 'no-cache';
    },
});

It seems deceptively simple for no one to have done it by now, is there a problem with this that happens not to affect my application, or should I open a PR?

@tgriesser
Copy link
Member

Check out #952

@Fryuni
Copy link
Author

Fryuni commented Jul 22, 2021

Niice!!

@Sytten Sytten added the type/feat Add a new capability or enhance an existing one label Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feat Add a new capability or enhance an existing one
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants