You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// import some metadata/types generated from schemaimport{generateSchemaInfo}from'schema.graphql';// Create a builder as usualconstbuilder=newSchemaBuilder({plugins: [...],});// Generate a new schema helperconstschema=builder.fromSchema(generateSchemaInfo,// refs provide a way to define TS types for you gql types// they can provide a place to add various plugin optionsrefs: {User: builder.objectRef<{name: string,id: string}>('User')// if no ref is provided for a type, a default type will be derived from shape of the schema});// Simple API can be used as a basic resolver mapschema.Query({fields: {// Just a resolverposts: (_,{ page })=>Posts.getPostsPage(page??0),// Or with optionsposts: {description: 'load some posts',resolve: (_,{ page })=>Posts.getPostsPage(page??0),}},});schema.User({// Fields can also be used more like a normal pothos field definitions// This enables interoperability with some more complicated pluginsfields: t=>({// t would have a custom method for each field defined in the schemaposts: t.posts({resolve: (user)=>Posts.getPostsByAuthor(user.id)}),// name resolver is needed because the ref has a name prop that matches the expected type for the name field}),});
Example with the prisma plugin
import{generateSchemaInfo}from'schema.graphql';constbuilder=newSchemaBuilder({plugins: [PrismaPlugin],});constschema=builder.fromSchema(generateSchemaInfo,refs: {User: builder.prismaObjectRef('User',{// prisma plugin specific options can be added here}),Post: builder.prismaObjectRef('Post',{}),});schema.Query({fields: t=>({posts: t.posts.prismaQuery(query=>prisma.posts.findMany(query)),}),});schema.User({fields: t=>({posts: t.posts.relation('posts'),name: ({ firstName, lastName })=>`${firstName}${lastName}`,}),});schema.Post({fields: t=>({author: t.author.relation('author'),}),});