-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.js
More file actions
54 lines (48 loc) · 1.64 KB
/
Copy pathstat.js
File metadata and controls
54 lines (48 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { printSchema } = require('graphql');
const { ApolloServer, gql } = require('apollo-server');
const { buildSubgraphSchema } = require('@apollo/subgraph'); // Optional for subgraphs
const fs = require('fs');
// Define your GraphQL schema
const typeDefs = gql`
directive @example on FIELD_DEFINITION
type Query {
getUsers(token: String!): User!
getCustodians(adminToken: String!): UsersOrMessage!
}
type UsersOrMessage {
message: String
users: [User!]
}
type User {
id: ID!
firstName: String!
lastName: String!
email: String!
password: String!
gender: String
username: String!
createdAt: String!
updatedAt: String!
}
`;
// Define resolvers (optional for schema generation)
const resolvers = {};
// Use buildSubgraphSchema if working with Apollo Federation, otherwise just use typeDefs
const schema = buildSubgraphSchema
? buildSubgraphSchema({ typeDefs, resolvers })
: new ApolloServer({ typeDefs, resolvers }).schema;
// Log and save the schema
try {
if (!schema) {
throw new Error("Schema is undefined. Ensure ApolloServer is properly initialized.");
}
// Log directives if available
const directives = schema.getDirectives ? schema.getDirectives() : [];
console.log("Directives:", directives.map((directive) => directive.name));
// Write the schema to a file
const schemaFilePath = './products-schema.graphql';
fs.writeFileSync(schemaFilePath, printSchema(schema));
console.log(`Schema successfully written to ${schemaFilePath}`);
} catch (error) {
console.error("Error generating schema:", error.message);
}