Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
graphql-extend-input
Advanced tools
Extend graphql Inputs.
The Simple answer is, extend input MyInput
is not part of the GraphQL specs.
So, let's say you need to add specific fields to an Input
on a remote fetched schema. In this case, you'll have to actually rewrite whole input schema, so now we have this duplication and it's really hard to maintain. This happens quite often if you are to build a graphql gateway and you need to merge the schemas from graphql microservices. Bonus feature extend enum
also available.
Hopefully this will be introduced into the specs sooner or later!
extend input MyInput
and NewInput extend input MyInput
to work.graphql/utilities
as much as possible.[NewName] extend <input|enum> ObjectToExt[, AnotherObjectToExt, ...]
NewInput extends input MyInput1, MyInput2, MyInput3 {}
. This will only return text (GraphQL language) with a new input
type called NewInput
.npm i graphql-extend-input --save-prod
import gqlExtI from 'graphql-extend-input';
// Schema representing a remote fetched schema
// this can either be a string or a GraphQLSchema object
const remoteFetchedSchema = `
scalar Date
input BookInput {
title: String!
date: Date
}
type Book {
id: ID!
title: String
author: String
price: Float
}
type Query {
getBooks(filter: [BookInput]!): Book
}
`;
const newSchema = gqlExtI(remoteFetchedSchema, `
extend input BookInput {
author: String
price: Float
}
`);
console.log(newSchema);
/** Output:
input BookInput {
author: String
price: Float
title: String!
date: Date
}
*/
So you can now use newSchema
and merge it with remoteFetchedSchema
. It would look something like:
import { mergeSchemas } from 'graphql-tools';
const mergedSchemas = mergeSchemas({
schemas: [
remoteFetchedSchema,
newSchema
],
resolvers: (mergeInfo) => ({
// new/overwrite resolvers
}),
onTypeConflict: (left, right) => {
// for this example we need to return right!
return right;
}
});
Input
If you don't need to mess with existing Inputs
you could also do the following.
const newSchema = gqlExtI(remoteFetchedSchema, `
NewBookInput extend input BookInput {
author: String
price: Float
}
extend type Query {
newGetBooks(filter: [NewBookInput]!): Book
}
`);
console.log(newSchema);
/** Output:
input NewBookInput {
author: String
price: Float
title: String!
date: Date
}
extend type Query {
newGetBooks(filter: [NewBookInput]!): Book
}
*/
const newSchema = gqlExtI(remoteFetchedSchema, `
extend input BookInput {
author: String
price: Float
}
`, true); // just need to pass true on the last param
console.log(newSchema);
/** Output:
scalar Date
input BookInput {
author: String
price: Float
title: String!
date: Date
}
type Book {
id: ID!
title: String
author: String
price: Float
}
type Query {
getBooks(filter: [BookInput]!): Book
}
*/
const remoteFetchedSchema = `
input PersonInput {
firstname: String!
lastname: String!
}
input AddressBookInput {
street: String!
phone: String!
}
`;
const newSchema = gqlExtI(remoteFetchedSchema, `
EmployeeInput extend input PersonInput, AddressBookInput {
salary: Float!
department: String!
}
extend type Query {
getEmployNumber(input: EmployeeInput): Int!
}
`);
console.log(newSchema);
/** Output:
input EmployeeInput {
salary: Float!
department: String!
street: String!
phone: String!
firstname: String!
lastname: String!
}
extend type Query {
getEmployNumber(input: EmployeeInput): Int!
}
*/
extend enum
const remoteFetchedSchema = `
input TVShowInput {
title: String!
duration: Int!
}
input TVShowCastInput {
castMemberName: String!
}
enum TVShows {
lost
walking_dead
}
enum BestTVShows {
got
breaking_bad
black_mirror
mr_robot
}
`;
const newSchema = gqlExtI( remoteFetchedSchema,
`
type TvShow {
title: String!
duration: Int!
cast: [String!]!
}
TVShowSearchInput extend input TVShowInput, TVShowCastInput {
tvshow: AllTVShowsEnum
}
AllTVShowsEnum extend enum TVShows, BestTVShows {}
extend type Query {
searchTVShows(input: TVShowSearchInput): [TVShow]
getTVShow(input: AllTVShowsEnum) TVShow
}
`
);
console.log(newSchema);
/** Output:
input TVShowSearchInput {
tvshow: AllTVShowsEnum
castMemberName: String!
title: String!
duration: Int!
}
enum AllTVShowsEnum {
got
breaking_bad
black_mirror
mr_robot
lost
walking_dead
}
type TvShow {
title: String!
duration: Int!
cast: [String!]!
}
extend type Query {
searchTVShows(input: TVShowSearchInput): [TVShow]
getTVShow(input: AllTVShowsEnum) TVShow
}
*/
extend enum
also or maybe that will be for another repo. ✔️Unfortunately I don't do open-source for a living, not that I would mind :), so if you find a bug or have any suggestion to improve this module you're very welcome to contribute.
FAQs
Allowing Input to be extended
The npm package graphql-extend-input receives a total of 1 weekly downloads. As such, graphql-extend-input popularity was classified as not popular.
We found that graphql-extend-input demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.