I created a MERN style app using GraphQL to handle all my data querying and mutating. I started out using jsx but now I'm trying to convert the app to typescript. Given my data is all coming from mongoDB I would prefer to use codegen to build my types based on the GraphQL schema.
When I built the app, I didn't use the typical typedefs and resolvers pattern but instead used mongoose models in my GraphQLSchema.
I'm struggling to get my codegen file to find my gql queries to build the generated types with. If anyone is familiar with this pattern, I would be grateful if I could find out if a) this is possible, and b) what I might be doing wrong (if possible). I included my best attempt at the file structure of the application. Please let me know if you need more info. I tried to be as concise as possible for clarity's sake. Note: All backend files are still .js files.
File structure
frontend (folder)
--src
----codegen.ts
----graphql (folder)
------queries
--------clientQueries.ts
backend (folder)
--schema (folder)
----schema.js
--models (folder)
----clientModel.js
codegen.ts
import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: "http://localhost:8000",
documents: ["src/graphql/*/*.ts"],
generates: {
"./src/__generated__/": {
preset: "client",
presetConfig: {
gqlTagName: "gql",
},
},
},
ignoreNoDocuments: true,
};
export default config;
clientQueries.ts
import { gql } from "@apollo/client";
const GET_CLIENTS = gql`
query getClients($organizationId: ID) {
clients(organizationId: $organizationId) {
id
firstName
lastName
}
}
`;
export { GET_CLIENTS };
schema.js
import Client from "../models/Client.js";
import {
GraphQLObjectType,
GraphQLID,
GraphQLString,
} from "graphql";
// Client Type
const ClientType = new GraphQLObjectType({
name: "Client",
fields: () => ({
id: { type: GraphQLID },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
}),
});
const RootQuery = new GraphQLObjectType({
fields: {
clients: {
type: new GraphQLList(ClientType),
args: { organizationId: { type: GraphQLID } },
resolve(parent, args) {
return Client.find({ organizationId: args.organizationId });
},
},
client: {
type: ClientType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return Client.findById(args.id);
},
},
}
})
const mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
// mutation code
}
})
const schema = new GraphQLSchema({
query: RootQuery,
mutation,
});
export { schema };
clientModel.js
import mongoose from "mongoose";
const ClientSchema = new mongoose.Schema({
firstName: {
type: String,
},
lastName: {
type: String,
},
});
const Client = mongoose.model("Client", ClientSchema);
export default Client;
Error picture Error picture