How to use the graphql-sequelize.defaultArgs function in graphql-sequelize

To help you get started, we’ve selected a few graphql-sequelize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github almost-full-stack / sequelize-graphql-schema / src / libs / generateTypes.js View on Github external
// Remove belongs to associations for input types to avoide foreign key constraint errors.
      if (!(isInput && relation.associationType === 'BelongsTo') && !(isInput && !nestedMutations)) {
        fields[associationName] = { type };
      }

      // Add through table, this doesn't need resolver since this is already included when quering n:m relations.
      if (relation.associationType === 'BelongsToMany' ) {
        fields[relation.through.model.name] = { type: existingTypes[relation.through.model.name] };
      }

      if (!isInput && !relation.isRemote) {

        const throughArguments = relation.associationType === 'BelongsToMany' ? { throughWhere: defaultListArgs().where } : {};

        // GraphQLInputObjectType do not accept fields with resolve
        fields[associationName].args = Object.assign(defaultArgs(relation), defaultListArgs(), throughArguments);
        fields[associationName].resolve = (source, args, context, info) => {
          return queryResolver(options)(relation, relation.target.name, source, args, context, info, true);
        };

      }

    }
  }

  return fields;
}
github almost-full-stack / sequelize-graphql-schema / src / libs / queries.js View on Github external
const modelType = outputTypes[modelTypeName];
        const queries = {
          [modelType.name + 'Default']: {
            type: GraphQLInt,
            description: 'An empty default Query.',
            resolve: () => 1
          }
        };
        const paranoidType = models[modelType.name].options.paranoid ? { paranoid: { type: GraphQLBoolean } } : {};

        const aliases = models[modelType.name].graphql.alias;

        if (models[modelType.name].graphql.excludeQueries.indexOf('query') === -1) {
          queries[camelCase(aliases.fetch || (modelType.name + 'Get'))] = {
            type: new GraphQLList(modelType),
            args: Object.assign(defaultArgs(models[modelType.name]), defaultListArgs(), includeArguments(), paranoidType),
            resolve: (source, args, context, info) => {
              return query(models[modelType.name], modelType.name, source, args, context, info);
            }
          }
        }

        if (models[modelTypeName].graphql && models[modelTypeName].graphql.queries) {

          for (const query in models[modelTypeName].graphql.queries) {

            if (models[modelTypeName].graphql.queries[query]) {
              let isArray = false;
              // eslint-disable-next-line no-unused-vars
              let isRequired = false;
              let outPutType = GraphQLInt;
              let inPutType = GraphQLInt;
github almost-full-stack / sequelize-graphql-schema / src / sequelize-graphql-schema.js View on Github external
if (!types[relation.target.name]) {
      return fields
    }

    // BelongsToMany is represented as a list, just like HasMany
    const type = relation.associationType === 'BelongsToMany' ||
    relation.associationType === 'HasMany'
      ? new GraphQLList(types[relation.target.name])
      : types[relation.target.name]

    fields[associationName] = { type }

    if (!isInput && !relation.isRemote) {
      // GraphQLInputObjectType do not accept fields with resolve
      fields[associationName].args = Object.assign(defaultArgs(relation), defaultListArgs(), includeArguments())
      fields[associationName].resolve = async (source, args, context, info) => {
        await execBefore(relation.target, source, args, context, info, 'fetch')
        const data = await resolver(relation, { [EXPECTED_OPTIONS_KEY]: dataloaderContext })(source, args, context, info)

        if (relation.target.graphql.extend.fetch && data.length) {
          const item = await relation.target.graphql.extend.fetch(data, source, args, context, info)
          return [].concat(item)
        }

        return data
      }
    } else if (!isInput && relation.isRemote) {
      fields[associationName].args = Object.assign({}, relation.query.args, defaultListArgs())
      fields[associationName].resolve = (source, args, context, info) => {
        return remoteResolver(source, args, context, info, relation.query, fields[associationName].args, types[relation.target.name])
      }
github almost-full-stack / sequelize-graphql-schema / src / sequelize-graphql-schema.js View on Github external
const modelType = outputTypes[modelTypeName]
      let queries = {
        [modelType.name + 'Default']: {
          type: GraphQLInt,
          description: 'An empty default Query.',
          resolve: () => 1
        }
      }
      const paranoidType = models[modelType.name].options.paranoid ? { paranoid: { type: GraphQLBoolean } } : {}

      const aliases = models[modelType.name].graphql.alias

      if (models[modelType.name].graphql.excludeQueries.indexOf('query') === -1) {
        queries[camelCase(aliases.fetch || (modelType.name + 'Get'))] = {
          type: new GraphQLList(modelType),
          args: Object.assign(defaultArgs(models[modelType.name]), defaultListArgs(), includeArguments(), paranoidType),
          resolve: (source, args, context, info) => {
            return queryResolver(models[modelType.name], modelType.name, source, args, context, info)
          }
        }
      };

      if (models[modelTypeName].graphql && models[modelTypeName].graphql.queries) {
        for (let query in models[modelTypeName].graphql.queries) {
          let isArray = false
          let isRequired = false
          let outPutType = GraphQLInt
          let inPutType = GraphQLInt
          let typeName = models[modelTypeName].graphql.queries[query].output
          let inputTypeNameField = models[modelTypeName].graphql.queries[query].input

          if (typeName) {
github almost-full-stack / sequelize-graphql-schema / src / libs / generateQueries.js View on Github external
const args = argsToFindOptions.default({ where });

            if (args.where) whereQueryVarsToValues(args.where, info.variableValues);

            return models[modelTypeName].count({
              where: args.where
            });
          },
          description: 'A count of the total number of objects in this connection, ignoring pagination.'
        };
      }

      if (!model.graphql.excludeQueries.includes('fetch') && isAvailable(exposeOnly.queries, [modelQueryName])) {
        queries[generateName(aliases.fetch || options.naming.queries, { type: naming.type.get, name: modelTypeName }, { pascalCase })] = {
          type: new GraphQLList(modelType),
          args: Object.assign(defaultArgs(model), defaultListArguments, includeArguments, paranoidType),
          resolve: (source, args, context, info) => {
            return query(model, modelType.name, source, args, context, info);
          }
        };
      }

      Object.assign(allCustomQueries, (model.graphql.queries || {}));

      return Object.assign(allQueries, queries);

    }, {});
github bradleyboy / tuql / src / builders / schema.js View on Github external
return fields;
        },
      });

      types[key] = type;

      queries[formatFieldName(key)] = {
        type: new GraphQLList(type),
        args: defaultListArgs(model),
        resolve: resolver(model),
      };

      queries[singular(formatFieldName(key))] = {
        type,
        args: defaultArgs(model),
        resolve: resolver(model),
      };

      mutations[`create${type}`] = {
        type,
        args: makeCreateArgs(model),
        resolve: async (obj, values, info) => {
          const thing = await model.create(values);
          return thing;
        },
      };

      mutations[`update${type}`] = {
        type,
        args: makeUpdateArgs(model),
        resolve: async (obj, values, info) => {