CombatCovid / backend-hardware-designs-registry

Similar to npm or yarn we want to have a registry of repositories with all the documentation and file downloads ready to use to replicate designs
GNU General Public License v2.0
0 stars 1 forks source link

Auto-Genetate Schemas. #10

Closed DNature closed 4 years ago

DNature commented 4 years ago

I was working on this project recently and @raahul1912 has done a lot. However, i think it's gonna be better to generate and merge schemas on the fly by scanning through the components directory for every file that ends with an extension type - i.e *.graphql then we wont have to bother about importing and adding a new schema into our array of schemas. there're a lot of tools to archive this.

i.e with merge-graphql-schemas and few other tools:

import { mergeResolvers, mergeTypes } from 'merge-graphql-schemas';
...

export const genSchema = (): GraphQLSchema => {
  const pathComponents = path.join(__dirname, '../components');
  const graphqlTypes = glob.sync(`${pathComponents}/**/*.graphql`).map((x) => fs.readFileSync(x, { encoding: 'utf8' }));

  const resolvers = glob.sync(`${pathComponents}/**/resolvers.?s`).map((resolver) => require(resolver).resolvers);

  return makeExecutableSchema({
    typeDefs: mergeTypes(graphqlTypes),
    resolvers: mergeResolvers(resolvers)
  });
};

The above code will save scale better than this though i've been using the array method.

const schema: GraphQLSchema = makeExecutableSchema({
  typeDefs: [postTypeDef, userTypeDef, commentsTypeDefs, ...],
  resolvers: [postResolvers, userResolvers, commentsResolvers, ...]
});
raahul1912 commented 4 years ago

@DNature you are right. I have tried this earlier but unfortunately not got success.

DNature commented 4 years ago

Alright. I got it to work.

How about generating namespaces for mutations to prevent using type any on every mutation argument?