Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prisma-archive/graphql-transform-schema
Transform, filter & alias resolvers of a GraphQL schema
https://github.com/prisma-archive/graphql-transform-schema
apollo graphql graphql-schema graphql-server
Last synced: about 1 month ago
JSON representation
Transform, filter & alias resolvers of a GraphQL schema
- Host: GitHub
- URL: https://github.com/prisma-archive/graphql-transform-schema
- Owner: prisma-archive
- Created: 2017-09-03T14:13:35.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-24T12:49:07.000Z (almost 6 years ago)
- Last Synced: 2024-10-06T06:50:41.091Z (about 1 month ago)
- Topics: apollo, graphql, graphql-schema, graphql-server
- Language: TypeScript
- Homepage: https://example-rakllksfme.now.sh/graphiql
- Size: 71.3 KB
- Stars: 84
- Watchers: 7
- Forks: 9
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
> `graphql-transform-schema` has been deprecated in favor of [schema transforms as part of `graphql-tools`](https://www.apollographql.com/docs/graphql-tools/schema-transforms)
# graphql-transform-schema [![npm version](https://badge.fury.io/js/graphql-transform-schema.svg)](https://badge.fury.io/js/graphql-transform-schema) [![Greenkeeper badge](https://badges.greenkeeper.io/graphcool/graphql-transform-schema.svg)](https://greenkeeper.io/)
Transform, filter & alias resolvers of a GraphQL schema
## Install
```sh
yarn add graphql-transform-schema
```## Usage
By default `transformSchema` passes through all queries/mutations. ([Open Demo](https://example-rakllksfme.now.sh/graphiql))
```ts
import { transformSchema } from 'graphql-transform-schema'// needed for remote schemas
import { createApolloFetch } from 'apollo-fetch'
import { makeRemoteExecutableSchema } from 'graphql-tools'const schema = await makeRemoteExecutableSchema(createApolloFetch({
uri: 'https://api.graph.cool/simple/v1/swapi',
}))// hide every query/mutation except the `Starship` and `allStarships` query
const transformedSchema = transformSchema(schema, {
'*': false,
Starship: true,
allStarships: true,
})const transformedSchema = transformSchema(schema, {
Query: {
'*': false,
Starship: true,
allStarships: true,
},
Mutation: {
},
Starship: {
'*': false,
id: true,
},
})
```### API
```ts
interface Rules {
[fieldName: string]: boolean | Function
}function transformSchema(schema: GraphQLSchema, rules: Rules): GraphQLSchema
```### Examples
#### Remove all `createX` and `deleteX` mutations
```ts
const transformedSchema = transformSchema(schema, {
Mutation: {
'create*': false,
'delete*': false
}
})
```#### Overwrite resolved data
```ts
const typeDefs = `
type Query {
hello: String!
}type Mutation {
alexaHello(name: String!): String!
}
`
const resolvers = {
Query: {
hello: () => 'Hello world',
},
Mutation: {
alexaHello: (_, { name }) => `Alexa: Hello world, ${name}`,
},
}
const schema = makeExecutableSchema({ typeDefs, resolvers })const transformedSchema = transformSchema(schema, {
alexaHello: ({ args, resolve }) => resolve(args).replace('Bob', 'Alice'),
})
```#### Overwrite arguments
```ts
const typeDefs = `
type Query {
hello: String!
}type Mutation {
alexaHello(name: String!): String!
}
`
const resolvers = {
Query: {
hello: () => 'Hello world',
},
Mutation: {
alexaHello: (_, { name }) => `Alexa: Hello world, ${name}`,
},
}
const schema = makeExecutableSchema({ typeDefs, resolvers })const transformedSchema = transformSchema(schema, {
alexaHello: ({ args, resolve }) => resolve({ name: 'John' }),
})
```## Next steps
- [ ] Alias/rename types and fields
- [ ] Transform field arguments
- [ ] Compose new queries/mutations out of existing queries/mutations## Help & Community [![Slack Status](https://slack.graph.cool/badge.svg)](https://slack.graph.cool)
Join our [Slack community](http://slack.graph.cool/) if you run into issues or have questions. We love talking to you!
![](http://i.imgur.com/5RHR6Ku.png)