NestJS GraphQL inconsistent autoSchemaFile and service sql - graphql

I'm using nestJS with ApolloFederationDriver. I also add some prefix with transformSchema option. The issue is that in sdl I can't see that prefix, but in the autoSchemaFile he is. It is a problem, because I can't create connection between this microservice and Gateway.
#Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
autoSchemaFile: 'src/graphql/schema.gql',
driver: ApolloFederationDriver,
plugins: [
ApolloServerPluginLandingPageLocalDefault(),
ApolloServerPluginInlineTraceDisabled(),
],
sortSchema: true,
transformSchema: <some_transforms>,
transformAutoSchemaFile: true,
}),
],
})
Example from the schema.gql file
type Query {
prefix_getSomeData(filter: prefix_FilterInput)
}
And the same query in the sdl(query ApolloGetServiceDefinition { _service { sdl } })
type Query {
getSomeData(filter: FilterInput)
}
How I can add prefix to the sdl file?

Related

Getting an error when having heuristic fragment matching with Apollo & Nuxt.js

I am trying to connect the below graphql query with nuxtjs.
query getContent($slug: String!) {
contents (filters: { slug: { eq: $slug } }) {
data {
id
attributes {
title
content {
__typename
... on ComponentContentParagraph {
content
}
}
}
}
}
}
I am getting the following error and not getting the result from the query.
You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types. Apollo Client will not be able to accurately map fragments. To make this error go away, use the `IntrospectionFragmentMatcher` as described in the docs: https://www.apollographql.com/docs/react/advanced/fragments.html#fragment-matcher
I have checked the questions and answers available here.
Apollo+GraphQL - Heuristic Fragment Manual Matching
I have followed the docs from apollo as well.
https://www.apollographql.com/docs/react/data/fragments/#fragment-matcher
I managed to generate possibleTypes as mentioned in the docs.
Here is my next config.
apollo: {
includeNodeModules: true,
clientConfigs: {
default: "~/graphql/default.js",
},
},
This is the default.js
import { InMemoryCache } from "apollo-cache-inmemory";
import possibleTypes from "./possibleTypes.json";
export default () => {
return {
httpEndpoint: process.env.BACKEND_URL || "http://localhost:1337/graphql",
cache: new InMemoryCache({ possibleTypes }),
};
};
I am using strapi for the backend and this query works fine when running from graphql interface.

Apollo Federation Gateway: include local schemas when composing supergraph

When composing a supergraph for Apollo Federation's gateway, you would create a .yaml config file with the routing urls to the subgraphs.
Ex: https://github.com/apollographql/supergraph-demo/blob/main/subgraphs/inventory/inventory.js
//supergraph.yaml
subgraphs:
inventory:
routing_url: http://inventory:4000/graphql
schema:
file: ./subgraphs/inventory/inventory.graphql
products:
routing_url: http://products:4000/graphql
schema:
file: ./subgraphs/products/products.graphql
users:
routing_url: http://users:4000/graphql
schema:
file: ./subgraphs/users/users.graphql
In the example above, they are starting an Apollo server for each subgraph and composing a supergraph.
Is it possible to compose a supergraph without starting Apollo servers and just including the local schemas?
You can. Following this tutorial: https://www.apollographql.com/blog/backend/using-apollo-federation-with-local-schemas/
Instead of using super and sub-graphs, use serviceList and conditionally build the data source.
const gateway = new ApolloGateway({
serviceList: [
{ name: "products", url: "http://localhost:4002" },
{ name: "countries", url: "http://countries" },
],
buildService: ({ url }) => {
if (url === "http://countries") {
return new LocalGraphQLDataSource(getCountriesSchema());
} else {
return new RemoteGraphQLDataSource({
url,
});
}
},
});

Multiple databases in Strapi

Is it possible to use two different databases, i.e MongoDB and Redis, simultaneously in Strapi?
I want to keep track of my refresh tokens by Redis, while all other documents on Mongo.
Yes, buit Strapi does not support Redis
module.exports = ({ env }) => ({
defaultConnection: 'default', // this defines the default connections
connections: {
default: { //that's the default connection
connector: 'bookshelf',
settings: {
client: 'sqlite',
filename: env('DATABASE_FILENAME', '.tmp/main.db'), //uses main db
},
options: {
useNullAsDefault: true,
},
},
messages: { //that's the connection for messages db
connector: 'bookshelf',
settings: {
client: 'sqlite',
filename: env('DATABASE_FILENAME', '.tmp/messages.db'), //uses another db
},
options: {
useNullAsDefault: true,
},
},
},
});

Gatsby-source-graphql requires option `fieldName` to be specified

I am trying to get a demo app that uses Hasura and Gatsby started (https://github.com/praveenweb/dynamic-jamstack-gatsby-hasura/tree/master/dynamic-auth-client).
I edited the gatsby-config.js file with my Hasura endpoint URL, but I get the following error.
ERROR
UNHANDLED REJECTION Type HASURA must define one or more fields.
Error: Type HASURA must define one or more fields.
gatsby-config.js
module.exports = {
siteMetadata: {
title: "projectname",
siteUrl: `https://www.myurlhere.com`,
},
plugins: [
`gatsby-plugin-react-helmet`,
`gatsby-plugin-sitemap`,
{
resolve: `gatsby-plugin-nprogress`,
options: {
// Setting a color is optional.
color: `tomato`,
// Disable the loading spinner.
showSpinner: false,
},
},
{
resolve: "gatsby-source-graphql",
options: {
typeName: "HASURA",
fieldName: "hasura",
url: "https://myurlhere.com/v1/graphql",
},
},
],
}
I found I only need to make the typeName: "Query" and the fieldName: "blah".
Error: Invariant Violation: gatsby-source-graphql requires option `typeName` to be specified
{
resolve: "gatsby-source-graphql",
options: {
// This type will contain remote schema Query type
typeName: "Query",
// This is field under which it's accessible
fieldName: "blah",
// Url to query from
url: "http://10.113.34.59:4000/graphql",
// this is URL where served exposed its service in local
},

Using GraphQl and MongoDB schema as library in NRWL/NX Enviornment

I am fairly new to the NRWL/NX world.
What I am trying to accomplish here is, to use the GraphQL (with the MongoDB) for the API.
In past, I've created the GraphQL project with MongoDB in a non-NRWL environment.
However since now we have multiple projects, we are moving to NX.
There are couple of MongoDB schema which are used across multiple projects, so I've decided to use them as a library. I generated a library and added following code
import { MongooseModule } from '#nestjs/mongoose';
import { ConfigService, ConfigModule } from '#another-lib/config-helper';
import { Module } from '#nestjs/common';
import { Location } from './model/location'; //This wouldn't be accessible from elsewhere
export const databaseProviders = [
MongooseModule.forRootAsync({
imports: [ ConfigModule ],
inject: [ ConfigService ],
useFactory: async (config: ConfigService) => ({
uri: config.get('MONGODB_URI'),
useNewUrlParser: true,
useFindAndModify: false,
}),
}),
];
#Module({
imports: [ ...databaseProviders, Location ],
exports: [ ...databaseProviders, Location ],
})
export class DatabaseModule {}
The MongoDB model is pretty standard.
import * as mongoose from 'mongoose';
const LocationSchema = new mongoose.Schema(
{
LocationName: {
type: String,
},
LocationCode: {
type: String,
},
isPickable: {
type: Boolean,
},
TemplateID: {
type: String,
},
},
{ collection: 'locations', timestamps: true },
);
export interface ILocation extends mongoose.Document {
_id: string;
LocationName: string;
LocationCode: string;
isPickable: boolean;
TemplateID: string;
}
//used for the server
export interface ILocationModel extends mongoose.Model<ILocation> {}
// export const LocationSchema = mongoose.model('location', _LocationSchema);
export const Location: ILocationModel = <ILocationModel>mongoose.model<ILocation>('Location', LocationSchema);
How can I access the mongodb model via DatabaseModule, Please suggest.
Thanks
-N Baua
Managed to solved the problem by setting up the correct directory structure and importing them in the datahelper.ts file.
We later exported the referenced interfaces from the datahelper.ts into the index.ts and we were done.
Thanks.

Resources