hayes / pothos

Pothos GraphQL is library for creating GraphQL schemas in typescript using a strongly typed code first approach
https://pothos-graphql.dev
ISC License
2.36k stars 164 forks source link

Printed Schema Doesn't Have @key Directives #1352

Closed rawkode closed 3 days ago

rawkode commented 3 days ago

I'm using the Drizzle plugin, so that may be what the problem is. However, I would expect that calling builder.asEntity would add the @key directive to the printed schema.

I've tried adding the directive manually to the type too, but alas that didn't work.

export const getSchema = (): GraphQLSchema => {
    const builder = new schemaBuilder<PothosTypes>({
        plugins: [directivesPlugin, drizzlePlugin, federationPlugin],
        directives: {
            useGraphQLToolsUnorderedDirectives: true,
        },
        drizzle: {
            client: db,
        },
    });

    const personRef = builder.drizzleObject("peopleTable", {
        name: "person",
                // I tried adding directives: [{name: "key", args: ...}],
        fields: (t) => ({
            id: t.exposeString("id"),
            forename: t.exposeString("forename"),
            surname: t.exposeString("surname"),
            email: t.exposeString("email", {
                requiresScopes: [["system"]],
            }),
        }),
    });

    builder.asEntity(personRef, {
        key: builder.selection<{ id: string }>("id"),
        resolveReference: async (user) =>
            await db.query.peopleTable.findFirst({
                where: eq(dataSchema.peopleTable.id, user.id),
            }).execute(),
    });

    builder.queryType({
        fields: (t) => ({
            me: t.drizzleField({
                type: personRef,
                resolve: async (query, _root, _args, ctx) =>
                    await db.query.peopleTable.findFirst(query({
                        where: eq(dataSchema.peopleTable.id, ctx.jwt.payload.sub),
                    })).execute(),
            }),
        }),
    });

    return builder.toSubGraphSchema({
        linkUrl: "https://specs.apollo.dev/federation/v2.6",
        federationDirectives: ["@key", "@authenticated", "@requiresScopes"],
    });
};
hayes commented 3 days ago

printSchema from the graphql package won't print directives. You need to use something else like https://the-guild.dev/graphql/tools/docs/api/modules/utils_src#printschemawithdirectives

rawkode commented 3 days ago

Oh, I had no idea. Thank you!