|
| 1 | +import { |
| 2 | + GraphQLSchema, |
| 3 | + GraphQLObjectType, |
| 4 | + GraphQLString, |
| 5 | + GraphQLNonNull, |
| 6 | + GraphQLList, |
| 7 | +} from "graphql" |
| 8 | + |
| 9 | +const PROJECT_NAME = "GraphQL" |
| 10 | +const PROJECT_TAGLINE = "A query language for APIs" |
| 11 | +const PROJECT_DESCRIPTION = |
| 12 | + "GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data." |
| 13 | +const PROJECT_WEBSITE = "https://graphql.org" |
| 14 | + |
| 15 | +export const INITIAL_QUERY_TEXT = `{ |
| 16 | + project(name: "${PROJECT_NAME}") { |
| 17 | + tagline |
| 18 | + } |
| 19 | +}` |
| 20 | + |
| 21 | +export const INITIAL_RESULTS_TEXT = `{ |
| 22 | + "project": { |
| 23 | + "tagline": "${PROJECT_TAGLINE}" |
| 24 | + } |
| 25 | +}` |
| 26 | + |
| 27 | +interface Project { |
| 28 | + name: string |
| 29 | + tagline: string |
| 30 | + description: string |
| 31 | + website: string |
| 32 | +} |
| 33 | + |
| 34 | +const projects: Project[] = [ |
| 35 | + { |
| 36 | + name: PROJECT_NAME, |
| 37 | + tagline: PROJECT_TAGLINE, |
| 38 | + description: PROJECT_DESCRIPTION, |
| 39 | + website: PROJECT_WEBSITE, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "GraphiQL", |
| 43 | + tagline: "Ecosystem for building browser & IDE tools.", |
| 44 | + description: |
| 45 | + "GraphiQL is the reference implementation of this monorepo, GraphQL IDE, an official project under the GraphQL Foundation. The code uses the permissive MIT license.", |
| 46 | + website: "https://github.com/graphql/graphiql", |
| 47 | + }, |
| 48 | +] |
| 49 | + |
| 50 | +const ProjectType = new GraphQLObjectType<Project>({ |
| 51 | + name: "Project", |
| 52 | + fields: { |
| 53 | + name: { |
| 54 | + type: GraphQLString, |
| 55 | + description: "The name of the project", |
| 56 | + }, |
| 57 | + tagline: { |
| 58 | + type: GraphQLString, |
| 59 | + description: "A short description of what the project does", |
| 60 | + }, |
| 61 | + description: { |
| 62 | + type: GraphQLString, |
| 63 | + description: "A detailed description of the project", |
| 64 | + }, |
| 65 | + website: { |
| 66 | + type: GraphQLString, |
| 67 | + description: "The project website URL", |
| 68 | + }, |
| 69 | + }, |
| 70 | +}) |
| 71 | + |
| 72 | +const QueryType = new GraphQLObjectType({ |
| 73 | + name: "Query", |
| 74 | + fields: { |
| 75 | + project: { |
| 76 | + type: ProjectType, |
| 77 | + args: { |
| 78 | + name: { |
| 79 | + type: new GraphQLNonNull(GraphQLString), |
| 80 | + description: "The name of the project to retrieve", |
| 81 | + }, |
| 82 | + }, |
| 83 | + resolve: (_, args) => { |
| 84 | + return projects.find( |
| 85 | + project => project.name.toLowerCase() === args.name.toLowerCase(), |
| 86 | + ) |
| 87 | + }, |
| 88 | + }, |
| 89 | + projects: { |
| 90 | + type: new GraphQLList(ProjectType), |
| 91 | + description: "Get all available projects", |
| 92 | + resolve: () => projects, |
| 93 | + }, |
| 94 | + }, |
| 95 | +}) |
| 96 | + |
| 97 | +export const projectsSchema = new GraphQLSchema({ |
| 98 | + query: QueryType, |
| 99 | +}) |
0 commit comments