graphql#IntrospectionInputTypeRef TypeScript Examples

The following examples show how to use graphql#IntrospectionInputTypeRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isDateTimeFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" &&
  [
    "DateTimeFilter",
    "DateTimeNullableFilter",
    "NestedDateTimeFilter",
    "NestedDateTimeNullableFilter",
  ].indexOf(type.name) !== -1
Example #2
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isBooleanFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" &&
  [
    "BoolFilter",
    "BoolNullableFilter",
    "NestedBoolFilter",
    "NestedBoolNullableFilter",
  ].indexOf(type.name) !== -1
Example #3
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isStringFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" &&
  ["StringFilter", "StringNullableFilter"].indexOf(type.name) !== -1
Example #4
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isNestedStringFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" &&
  ["NestedStringFilter", "NestedStringNullableFilter"].indexOf(type.name) !==
    -1
Example #5
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isIntFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" &&
  [
    "IntFilter",
    "IntNullableFilter",
    "NestedIntFilter",
    "NestedIntNullableFilter",
  ].indexOf(type.name) !== -1
Example #6
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isFloatFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" &&
  [
    "FloatFilter",
    "FloatNullableFilter",
    "NestedFloatFilter",
    "NestedFloatNullableFilter",
  ].indexOf(type.name) !== -1
Example #7
Source File: buildWhere.ts    From ra-data-prisma with MIT License 5 votes vote down vote up
isEnumFilter = (type: IntrospectionInputTypeRef) =>
  type.kind === "INPUT_OBJECT" && type.name.startsWith("Enum")
Example #8
Source File: buildData.ts    From ra-data-prisma with MIT License 4 votes vote down vote up
buildNewInputValue = (
  fieldData: any,
  previousFieldData: any,
  fieldName: string,
  fieldType: IntrospectionInputTypeRef,
  introspectionResults: IntrospectionResult,
) => {
  const kind = fieldType.kind;

  switch (kind) {
    case "SCALAR":
    case "ENUM": {
      // if its a date, convert it to a date
      if (fieldType.kind === "SCALAR" && fieldType.name === "DateTime") {
        return new Date(fieldData);
      }
      if (fieldType.kind === "SCALAR" && fieldType.name === "String") {
        if (isObject(fieldData)) {
          return JSON.stringify(fieldData);
        }
      }
      return fieldData;
    }
    case "INPUT_OBJECT": {
      const fieldObjectType = fieldType as IntrospectionInputObjectType;

      const fullFieldObjectType = introspectionResults.types.find(
        (t) => t.name === fieldObjectType.name,
      ) as IntrospectionInputObjectType;

      const setModifier = fullFieldObjectType?.inputFields.find(
        (i) => i.name === ModifiersParams.set,
      );

      const connectModifier = fullFieldObjectType?.inputFields.find(
        (i) => i.name === ModifiersParams.connect,
      );

      const disconnectModifier = fullFieldObjectType?.inputFields.find(
        (i) => i.name === ModifiersParams.disconnect,
      );

      const deleteModifier = fullFieldObjectType?.inputFields.find(
        (i) => i.name === ModifiersParams.delete,
      );

      if (setModifier && !connectModifier && !disconnectModifier && !deleteModifier) {
        // if its a date, convert it to a date
        if (
          setModifier.type.kind === "SCALAR" &&
          setModifier.type.name === "DateTime"
        ) {
          return { set: new Date(fieldData) };
        }
        return { set: fieldData };
      }

      const isRelationship = fullFieldObjectType?.inputFields.every((i) => {
        return Object.keys(ModifiersParams).includes(i.name);
      });

      // is it a relation?
      if (isRelationship) {
        // if it has a set modifier, it is an update array
        const createModifier = fullFieldObjectType?.inputFields.find(
          (i) => i.name === ModifiersParams.create,
        );

        const updateModifier = fullFieldObjectType?.inputFields.find(
          (i) => i.name === ModifiersParams.update,
        );

        const isList = fullFieldObjectType?.inputFields.some((i) => {
          return i.type.kind === "LIST";
        });

        if (isList) {
          if (Array.isArray(fieldData)) {
            const createListInputType = getCreateInputDataTypeForList(
              createModifier,
              introspectionResults,
            );

            const updateListInputType = updateModifier
              ? getUpdateInputDataTypeForList(
                  updateModifier,
                  introspectionResults,
                )
              : null;

            const variables = fieldData.reduce<UpdateManyInput>(
              (inputs, referencedField) => {
                if (isObject(referencedField)) {
                  // TODO: we assume "data.id" to be the id
                  if (isObjectWithId(referencedField)) {
                    const connectRelation = !previousFieldData?.find((p) =>
                      p.id
                        ? p.id === referencedField.id
                        : p === referencedField.id,
                    );
                    if (!updateListInputType || connectRelation) {
                      inputs.connect = [
                        ...(inputs.connect || []),
                        { id: referencedField.id },
                      ];
                    } else {
                      // update
                      const data = buildData(
                        updateListInputType,
                        {
                          id: referencedField.id,
                          data: referencedField,
                          previousData: previousFieldData?.find(
                            (previousField) => {
                              // TODO: we assume ".id" to be the id
                              return previousField.id === referencedField.id;
                            },
                          ),
                        },
                        introspectionResults,
                      );
                      if (Object.keys(data).length) {
                        inputs.update = [
                          ...(inputs.update || []),
                          { where: { id: referencedField.id }, data },
                        ];
                      }
                    }
                  } else {
                    // create
                    const data = buildData(
                      createListInputType,
                      {
                        data: referencedField,
                      },
                      introspectionResults,
                    );
                    inputs.create = [...(inputs.create || []), data];
                  }
                } else {
                  // only reference id's
                  // what about multiple id's for one reference?
                  if (
                    !previousFieldData?.find((p) => {
                      // TODO: we assume "p.id" to be the id
                      return p.id
                        ? p.id === referencedField
                        : p === referencedField;
                    })
                  ) {
                    inputs.connect = [
                      ...(inputs.connect || []),
                      { id: referencedField },
                    ];
                  }
                }
                return inputs;
              },
              {},
            );

            // disconnect the ones that are not referenced anymore
            if (Array.isArray(previousFieldData)) {
              const removableRelations = (previousFieldData as any[]).reduce<
                Array<{ id: any }>
              >((inputs, data) => {
                // TODO: we assume "data.id" to be the id
                const dataId = data.id || data;
                if (
                  !fieldData.find((p) => {
                    // TODO: we assume "p.id" to be the id
                    const pId = p.id || p;
                    return pId === dataId;
                  })
                ) {
                  return [...(inputs || []), { id: dataId }];
                }
                return inputs;
              }, []);
              if (removableRelations.length) {
                if (disconnectModifier) {
                  variables.disconnect = removableRelations;
                } else if (deleteModifier) {
                  variables.delete = removableRelations;
                }
              }
            }
            return variables;
          } else {
            throw new Error(`${fieldName} should be an array`);
          }
        } else {
          if (!fieldData) {
            if (disconnectModifier) {
              return {
                disconnect: true,
              };
            } else if (deleteModifier) {
              return {
                delete: true,
              };
            }
          }

          if (isObject(fieldData)) {
            if (!isObjectWithId(fieldData)) {
              if (!createModifier) {
                return;
              }
              // TODO: we assume ".id" to be the id
              const createObjectModifierType = getFinalType(
                createModifier.type,
              );
              const createObjectInputType = introspectionResults.types.find(
                (t) => t.name === createObjectModifierType.name,
              ) as IntrospectionInputObjectType;

              // create
              const data = buildData(
                createObjectInputType,
                {
                  data: fieldData,
                },
                introspectionResults,
              );
              return { create: data };
            } else {
              if (previousFieldData?.id === fieldData.id) {
                if (!updateModifier) {
                  return;
                }
                const updateObjectModifierType = getFinalType(
                  updateModifier.type,
                );
                const updateObjectInputType = introspectionResults.types.find(
                  (t) => t.name === updateObjectModifierType.name,
                ) as IntrospectionInputObjectType;

                // update
                const data = buildData(
                  updateObjectInputType,
                  {
                    id: fieldData.id,
                    data: fieldData,
                    previousData: previousFieldData,
                  },
                  introspectionResults,
                );
                return { update: data };
              } else if (connectModifier) {
                return { connect: { id: fieldData.id } };
              }
            }
          } else if (connectModifier) {
            return { connect: { id: fieldData } };
          }
        }
      } else {
        return fieldData;
      }

      return;
    }
    case "LIST":
    case "NON_NULL":
      return;
    default:
      exhaust(kind);
  }
}