remix-graphql icon indicating copy to clipboard operation
remix-graphql copied to clipboard

use .graphql files

Open seanaguinaga opened this issue 2 years ago • 3 comments

Hello

Can we use .graphql files for the schema?

seanaguinaga avatar Feb 10 '22 22:02 seanaguinaga

Hello @seanaguinaga 👋 you definitely can! This isn't built into remix-graphql per se, but it should be straight-forward to make this work using makeExecutableSchema from the @graphql-tools/schema package (see here).

It could look something like this:

# File ~/app/graphql/schema.graphql

type Query {
  hello: String
}
// File ~/app/graphql/schema.server.ts

import { makeExecutableSchema } from "@graphql-tools/schema";
import fs from "fs";
import path from "path";

// Load the type definitions from the file system
const typeDefs = fs.readSync(path.join(__dirname, "schema.graphql"));

// Define resolvers
const resolvers = {
  Query: {
    hello() {
      return "world";
    },
  },
};

// Combine both into a schema and export that for usage with `remix-graphql`
export const schema = makeExecutableSchema({
  typeDefs,
  resolvers
});

thomasheyenbrock avatar Feb 11 '22 09:02 thomasheyenbrock

@seanaguinaga did that work out for you? 👆 🙂

I think this is a common pattern (especially when using apollo), would you be up to adding a section in the README about that?

thomasheyenbrock avatar Feb 18 '22 10:02 thomasheyenbrock

@thomasheyenbrock gonna try it today!

Yeah I would!

Let me give it a go

seanaguinaga avatar Feb 20 '22 15:02 seanaguinaga