GraphQL schema use case and functions

add new field or rename an existing field.)You could use API gateway tools but management of many APIs is a tough thing.Useful Functions: COPY, BLEND and CUSTOMIZE schema in GraphQL worldGraphQL and some tools prepare useful functions for copying, blending and customizing API.[Preliminary] Schema introspectionRemote Schema for API copyingSchema Stitching for API blendingSchema Delegation for API copying and customizationSchema Transform for API customization (modification)Preliminary knowledge: Schema Inspection in GraphQLIn GraphQL, you only define data type with GraphQL Schema Language, then GraphQL server provides APIs and documents automatically and publishes it. For example, a service Nobita defines the following data type.GraphQL Schema Language includes data names, types and descriptions. This information is reformed as a format of API specification document which called Introspection)..You can request this information to GraphQL server with special query __schema.GraphQL schema creates API and document (__schema) automatically.Introspection enables GraphQL servers and clients to share schema and verify queries.Function 1 (COPY an API) “Remote Schema”Remote schema fetches API schema from remote GraphQL server????.Remote schema enables Gian to copy Nobita’s API schema and reproduce it.When Nobita web service is publishing its API document,graphql-tools makeRemoteExecutableSchema(→ref) can get API document and reproduce API..This function receives remote GraphQL server URL as argument.introspectSchema is used together in usual case.Web service Gian..(gian.ts)makeRemoteExecutableSchema in graphql-tools fetches Nobita’s API document (__schema) and generates executable API and this own document in this server.????This web server starts with the following lines????Starting web service GianFunction 2 (Blend APIs) “Schema Stitching”Blending Nobita’s APIs and Suneo’s APIs (Schema stitching) ????mergeSchemas of graphql-tools(→ref) works as schema stitching tool in excellent way.ProcessGet schema of Nobita and Suneo with remote schema (previous section)pass array of schema into mergeSchemas argument schemasNow we have a new service including existing multiple APIs ????Function 3 (Copy and Customize an API) “Schema Delegation”When you want to copy API schema and customize a little bit (ex. rename query field), you can use Schema Delegation(→ref) of graphql-tools.You can use delegateToSchema in query resolver, then you let any requests on your server transferred to the designated server and query.The following example shows original manga query renamed to another name orenomanga????Function 4 (Modify an API) “Schema Transform”When you want to customize an existing API a lot (e.g. remove query field), you can use Schema Transforms(→ref) of graphql-tools.The following example implies the original query mangais removed by Gian????The second argument of transformSchema is transformation type..In this example, it is set to FilterRootFields, which means “field removal”.Other transformsFilterTypes / remove type/ use sometimesRenameTypes / rename type/ use sometimesTransformRootFields, RootField / modify one of Query, Mutation, Subscription / use rarelyFilterRootFields / remove fields/ use mostlyRenameRootFields / rename fields/use mostlyExtractField / change path / don’t know much…WrapQuery/ maybe almighty / don’t know much…SummaryGraphQL and graphql-tools are excellent tools????Copy/ makeRemoteExecutableSchema, introspectSchema / so usefulBlend/ mergeSchemas / usefulCustomize/ transformSchema / maybe usefulI didn’t mention about Schema Directives..You can restrict access with authentication.Remaining problemsYou may encounter new problems if you reuse API.N+1 problem⌛️Sometimes, performance problems occur..Schema stitching may bring a result of N+1 requests from one request..Dataloader for batching of queries is effective for this kind of problems..Dataloader was developed by GraphQL co-developer Lee Byron from Facebook.Copyright problem????When you use APIs of any other persons, you must obey rules..For example, the name of the author must be stated when reused APIs requires creative commons cc-by..You can add metadata to GraphQL response and customize with middlewares.SummaryGraphQL tools (remote schema and schema stitching) enable copying, blending and customizing API..It’s easy.. More details

Leave a Reply