dapalex / py-graphql-mapper

A python library for generating GraphQL queries and mutations using plain python objects, no hardcoded strings.Any questions or issues, please report here https://github.com/dapalex/py-graphql-mapper/issues
MIT License
19 stars 1 forks source link

Unwanted Recursion In Generated Classes #24

Closed danieledwardgeorgehitchcock closed 11 months ago

danieledwardgeorgehitchcock commented 11 months ago

I am using this tool to create a Python Module see here. This is using the open GraphQL API instance here.

I seem to be having issues regarding the recursion of fields in comparison to what I am expecting to see from the documented queries in the GraphQL client referenced above.

An example of this is as follows:

Expected query:

query Me {
  me {
    id
    firstName
    lastName
    email
    phoneNumber
    isSuperuser
    isStaff
    isGuest
    isActive
    isEmailVerified
    dateJoined
    groups {
      id
      name
      permissions {
        id
        codename
        name
        __typename
      }
      __typename
    }
    referred {
      id
      user {
        id
        firstName
        lastName
        dateJoined
        member {
          id
          __typename
        }
        __typename
      }
      recommendedBy {
        id
        __typename
      }
      __typename
    }
    payments {
      id
      paid
      amount
      __typename
    }
    directDebit {
      id
      accountName
      accountSortCode
      accountNumber
      paymentDay
      __typename
    }
    member {
      id
      registrationCompleted
      __typename
    }
    __typename
  }
}

Query from generator:

query Me {
  me {
    id
    firstName
    lastName
    groups {
      id
      name
      permissions {
        id
        name
        codename
      }
    }
    isStaff
    isActive
    isGuest
    dateJoined
    isEmailVerified
    createdAt
    updatedAt
    email
    phoneNumber
    member {
      id
      registrationCompleted
      communityName
      isBusiness
      businessName
      businessRegistrationNumber
      fullyChargedPreregistered
      badges {
        id
        name
        code
        description
        imageUrl
      }
      branding {
        id
        createdAt
        updatedAt
        cssClassName
        displayName
        heroImage {
          id
          name
          description
          url
        }
        howItWorksImage {
          id
          name
          description
          url
        }
        howDoesItWorkPanelImageFirst {
          id
          name
          description
          url
        }
        howDoesItWorkPanelImageSecond {
          id
          name
          description
          url
        }
        howDoesItWorkPanelImageThird {
          id
          name
          description
          url
        }
        supplier {
          id
          name
          isRipplePartner
          highlights
          logoUrl
        }
        signUpState {
          id
          createdAt
          updatedAt
          address {
            id
            town
            country
            line1
            line2
            county
            postcode
            mpan
            mprn
            manuallyEntered
            requiresManualSwitch
            hasPrepaidMeter
            selectedFromDropdown {
              line1
              line2
              line3
              town
              county
              postcode
              mpans
              mprn
              gsp
              economy7
              prepaidElectricityMeter
              display
            }
          }
          consumption {
            id
            approvalStatus
            evidenceSubmissions {
              id
              createdAt
              status
              approvedEvidence
              reviewEvidence
              memberNote
              evidences {
                id
                createdAt
                consumption
                name
                file
                fileSizeFormatted
                fileUrl
                contentType
              }
              adminNote
              approvedDate
              approvedBy {
                id
                firstName
                lastName
                email
                phoneNumber
              }
            }
            latestSubmission {
              id
              createdAt
              status
              approvedEvidence
              reviewEvidence
              memberNote
              evidences {
                id
                createdAt
                consumption
                name
                file
                fileSizeFormatted
                fileUrl
                contentType
              }
              adminNote
              approvedDate
              approvedBy {
                id
                firstName
                lastName
                email
                phoneNumber
              }
            }
            expectedGenerationKwh
            totalApprovedConsumptionKwh
            totalUnapprovedConsumptionKwh
          }
          quote {
            id
            currency {
              id
              code
              precision
              symbol
            }
            coop {
              id
              currency {
                id
                code
                precision
                symbol
              }
              name
              code
              description
              generationfarm {
                id
                currency {
                  id
                  code
                  precision
                  symbol
                }
                name
                address {
                  id
                  town
                  country
                }
                longitude
                latitude
                isLocationConfirmed
                model
                capacity
                generationType
                operationalStatus
                startDate
                generationData {
                  title
                  dataSet {
                    netPowerOutputKwh
                    dateTime
                    isForecastData
                    savingsForPeriod
                  }
                }
                lightShowImage
                capacityToGenerationFactor
                insightsChartData {
                  chartData {
                    windSpeedMph
                    generationKwh
                    savings
                    fromTime
                    toTime
                  }
                  cumulativeData {
                    cumulativeSavings
                    cumulativeGenerationKwh
                    averageWindSpeedMph
                  }
                  input {
                    genFarmId
                    period
                    startDate
                    endDate
                  }
                  userErrors {
                    field
                    message
                  }
                }
                latestGenerationDataPoint {
                  id
                  timestamp
                  windSpeedAvg
                  generatorSpeedAvg
                  bladeAngleAvg
                  nacellePositionNorth
                }
              }
              status
              wattageSku
              active
              firstYearEstimatedBillSavingsPerWattHour
              estimatedBillSavingsPerWattHour
              maxCapacityPerMember
              minCapacityPerMember
              publicCloseDate
              percentageFunded
              timelineProgression
              isAvailableForLocalPurchase
              documents {
                id
                document {
                  id
                  updatedAt
                  name
                  category
                  subcategory
                  description
                  documentTags {
                    id
                    name
                  }
                }
                version
                documentUrl
              }
              maxGenerationPerMember
              minGenerationPerMember
              costTotalPerWattHour
              localPurchasePostcodes
            }
            capacity
            ownershipMultiplier
            capacityCost
            arrangementFee
            vouchers {
              id
              code
              expiry
              displayName
              payload {
                code
              }
              error
              valid
              allocated
            }
            costTotal
            costTotalLocale
            electricityConsumptionAnnual
            expectedGenerationAnnual
            paymentTotal
            paymentTotalLocale
            instalments
            instalmentAmount1
            instalmentAmount1Locale
            instalmentAmountN
            instalmentAmountNLocale
            vouchersAmount
            vouchersAmountLocale
            billSavingsAnnualEstimate
            co2SavingsAnnualEstimate
            plantedTreesSavingsAnnualEstimate
            shareCount
            arrangementFeeLocale
            arrangementFeeTax
            arrangementFeeTaxLocale
            arrangementFee1
            arrangementFeeLocale1
            arrangementFeeTax1
            arrangementFeeTaxLocale1
          }
          supplierQuotes {
            id
            supplier {
              id
              name
              isRipplePartner
              highlights
              logoUrl
            }
            isAlreadyWithPartnerSupplier
            isAlreadyWithBrandedSupplier
            wantsToSeeTariffs
            switchesToAnotherPartnerSupplier
            electricitySupplierId
            postcode
            hasEconomy7Meter
            electricityAnnualDayKwh
            electricityAnnualNightKwh
            gasAnnualKwh
            supplierQuotesJson
            selectedQuoteCode
            switchesGas
            requiresManualSwitch
            isSwitchDelayed
            switchFailed
            switchDisabled
            delayedSwitchDate
            initiated
            acknowledgesTariffsWillBeDifferentInTheFuture
            acknowledgesHeWillLoseSavingsIfHeDoesntSwitchOnTime
            acknowledgesHeNeedsToContactTheSupplierToUpdateAddress
            selectedTariff
            currency {
              id
              code
              precision
              symbol
            }
            electricityAnnualStandardKwh
          }
        }
      }
      beneficiaries {
        id
        nominatedPerson
        email
        phone
        line1
        line2
        town
        postcode
      }
      address {
        id
        town
        country
        line1
        line2
        county
        postcode
        mpan
        mprn
        manuallyEntered
        requiresManualSwitch
        hasPrepaidMeter
        selectedFromDropdown {
          line1
          line2
          line3
          town
          county
          postcode
          mpans
          mprn
          gsp
          economy7
          prepaidElectricityMeter
          display
        }
      }
      consumption {
        id
        fromCalculator
        peopleCount
        bedroomsCount
        hasElectricVehicle
        hasElectricHeating
        hasHeatPump
        hasSolarPanels
        solarPanelsCapacity
        acknowledgesToProvideBillEvidence
        electricityAnnualKwh
      }
      invoices {
        id
        quote {
          id
          currency {
            id
            code
            precision
            symbol
          }
          coop {
            id
            currency {
              id
              code
              precision
              symbol
            }
            name
            code
            description
            generationfarm {
              id
              currency {
                id
                code
                precision
                symbol
              }
              name
              address {
                id
                town
                country
              }
              longitude
              latitude
              isLocationConfirmed
              model
              capacity
              generationType
              operationalStatus
              startDate
              generationData {
                title
                dataSet {
                  netPowerOutputKwh
                  dateTime
                  isForecastData
                  savingsForPeriod
                }
              }
              lightShowImage
              capacityToGenerationFactor
              insightsChartData {
                chartData {
                  windSpeedMph
                  generationKwh
                  savings
                  fromTime
                  toTime
                }
                cumulativeData {
                  cumulativeSavings
                  cumulativeGenerationKwh
                  averageWindSpeedMph
                }
                input {
                  genFarmId
                  period
                  startDate
                  endDate
                }
                userErrors {
                  field
                  message
                }
              }
              latestGenerationDataPoint {
                id
                timestamp
                windSpeedAvg
                generatorSpeedAvg
                bladeAngleAvg
                nacellePositionNorth
              }
            }
            status
            wattageSku
            active
            firstYearEstimatedBillSavingsPerWattHour
            estimatedBillSavingsPerWattHour
            maxCapacityPerMember
            minCapacityPerMember
            publicCloseDate
            percentageFunded
            timelineProgression
            isAvailableForLocalPurchase
            documents {
              id
              document {
                id
                updatedAt
                name
                category
                subcategory
                description
                documentTags {
                  id
                  name
                }
              }
              version
              documentUrl
            }
            maxGenerationPerMember
            minGenerationPerMember
            costTotalPerWattHour
            localPurchasePostcodes
          }
          capacity
          ownershipMultiplier
          capacityCost
          arrangementFee
          vouchers {
            id
            code
            expiry
            displayName
            payload {
              code
            }
            error
            valid
            allocated
          }
          costTotal
          costTotalLocale
          electricityConsumptionAnnual
          expectedGenerationAnnual
          paymentTotal
          paymentTotalLocale
          instalments
          instalmentAmount1
          instalmentAmount1Locale
          instalmentAmountN
          instalmentAmountNLocale
          vouchersAmount
          vouchersAmountLocale
          billSavingsAnnualEstimate
          co2SavingsAnnualEstimate
          plantedTreesSavingsAnnualEstimate
          shareCount
          arrangementFeeLocale
          arrangementFeeTax
          arrangementFeeTaxLocale
          arrangementFee1
          arrangementFeeLocale1
          arrangementFeeTax1
          arrangementFeeTaxLocale1
        }
        category
        invoiceUrl
        description
        gross
        net
      }
      capacityOwnedTotal
      ownershipPercentageTotal
      memberDocument {
        id
        createdAt
        category
        comments
        file
        documentUrl
      }
      investmentTotal
      billSavingsAcrossAllProjects
      expectedGenerationAnnualTotal
      hasReservedInActiveCoop
      dateOfReservationInActiveCoop
      dateOfLastSharePurchase
      hasBoughtSharesInActiveCoop
      hasBoughtSharesInGraigFatha
      hasBoughtSharesInKirkHill
      hasBoughtSharesInMultipleCoops
      isSuppliedByOctopusEnergy
      currentMemberSupplier {
        id
        supplier {
          id
          name
          isRipplePartner
          highlights
          logoUrl
        }
        accountIdentifier
        personalApiKey
        octopusMpan
        octopusMeterSerialNumber
        isOctopusApiConnectionVerified
        wantsToSeeOctopusApiConsumptionData
      }
      waitingListPlaces {
        id
        waitingList {
          id
          coop {
            id
            currency {
              id
              code
              precision
              symbol
            }
            name
            code
            description
            generationfarm {
              id
              currency {
                id
                code
                precision
                symbol
              }
              name
              address {
                id
                town
                country
              }
              longitude
              latitude
              isLocationConfirmed
              model
              capacity
              generationType
              operationalStatus
              startDate
              generationData {
                title
                dataSet {
                  netPowerOutputKwh
                  dateTime
                  isForecastData
                  savingsForPeriod
                }
              }
              lightShowImage
              capacityToGenerationFactor
              insightsChartData {
                chartData {
                  windSpeedMph
                  generationKwh
                  savings
                  fromTime
                  toTime
                }
                cumulativeData {
                  cumulativeSavings
                  cumulativeGenerationKwh
                  averageWindSpeedMph
                }
                input {
                  genFarmId
                  period
                  startDate
                  endDate
                }
                userErrors {
                  field
                  message
                }
              }
              latestGenerationDataPoint {
                id
                timestamp
                windSpeedAvg
                generatorSpeedAvg
                bladeAngleAvg
                nacellePositionNorth
              }
            }
            status
            wattageSku
            active
            firstYearEstimatedBillSavingsPerWattHour
            estimatedBillSavingsPerWattHour
            maxCapacityPerMember
            minCapacityPerMember
            publicCloseDate
            percentageFunded
            timelineProgression
            isAvailableForLocalPurchase
            documents {
              id
              document {
                id
                updatedAt
                name
                category
                subcategory
                description
                documentTags {
                  id
                  name
                }
              }
              version
              documentUrl
            }
            maxGenerationPerMember
            minGenerationPerMember
            costTotalPerWattHour
            localPurchasePostcodes
          }
        }
        generationRequestedKwh
      }
    }
    payments {
      id
      updatedAt
      currency {
        id
        code
        precision
        symbol
      }
      amount
      description
      category
      failed
      paid
      refunded
      dueAt
      amountLocale
      memberInvoices {
        id
        quote {
          id
          currency {
            id
            code
            precision
            symbol
          }
          coop {
            id
            currency {
              id
              code
              precision
              symbol
            }
            name
            code
            description
            generationfarm {
              id
              currency {
                id
                code
                precision
                symbol
              }
              name
              address {
                id
                town
                country
              }
              longitude
              latitude
              isLocationConfirmed
              model
              capacity
              generationType
              operationalStatus
              startDate
              generationData {
                title
                dataSet {
                  netPowerOutputKwh
                  dateTime
                  isForecastData
                  savingsForPeriod
                }
              }
              lightShowImage
              capacityToGenerationFactor
              insightsChartData {
                chartData {
                  windSpeedMph
                  generationKwh
                  savings
                  fromTime
                  toTime
                }
                cumulativeData {
                  cumulativeSavings
                  cumulativeGenerationKwh
                  averageWindSpeedMph
                }
                input {
                  genFarmId
                  period
                  startDate
                  endDate
                }
                userErrors {
                  field
                  message
                }
              }
              latestGenerationDataPoint {
                id
                timestamp
                windSpeedAvg
                generatorSpeedAvg
                bladeAngleAvg
                nacellePositionNorth
              }
            }
            status
            wattageSku
            active
            firstYearEstimatedBillSavingsPerWattHour
            estimatedBillSavingsPerWattHour
            maxCapacityPerMember
            minCapacityPerMember
            publicCloseDate
            percentageFunded
            timelineProgression
            isAvailableForLocalPurchase
            documents {
              id
              document {
                id
                updatedAt
                name
                category
                subcategory
                description
                documentTags {
                  id
                  name
                }
              }
              version
              documentUrl
            }
            maxGenerationPerMember
            minGenerationPerMember
            costTotalPerWattHour
            localPurchasePostcodes
          }
          capacity
          ownershipMultiplier
          capacityCost
          arrangementFee
          vouchers {
            id
            code
            expiry
            displayName
            payload {
              code
            }
            error
            valid
            allocated
          }
          costTotal
          costTotalLocale
          electricityConsumptionAnnual
          expectedGenerationAnnual
          paymentTotal
          paymentTotalLocale
          instalments
          instalmentAmount1
          instalmentAmount1Locale
          instalmentAmountN
          instalmentAmountNLocale
          vouchersAmount
          vouchersAmountLocale
          billSavingsAnnualEstimate
          co2SavingsAnnualEstimate
          plantedTreesSavingsAnnualEstimate
          shareCount
          arrangementFeeLocale
          arrangementFeeTax
          arrangementFeeTaxLocale
          arrangementFee1
          arrangementFeeLocale1
          arrangementFeeTax1
          arrangementFeeTaxLocale1
        }
        category
        invoiceUrl
        description
        gross
        net
      }
      invoices {
        id
        createdAt
        description
        net
        gross
        refunded
        category
        invoiceUrl
        invoiceType
      }
      paymentType
      status
      project
      date
    }
    directDebit {
      id
      createdAt
      updatedAt
      accountName
      accountSortCode
      accountNumber
      paymentDay
    }
    referred {
      id
    }
    isSuperuser
    apiKey
    consumptionApproval {
      id
      approvalStatus
      evidenceSubmissions {
        id
        createdAt
        status
        approvedEvidence
        reviewEvidence
        memberNote
        evidences {
          id
          createdAt
          consumption
          name
          file
          fileSizeFormatted
          fileUrl
          contentType
        }
        adminNote
        approvedDate
        approvedBy {
          id
          firstName
          lastName
          email
          phoneNumber
        }
      }
      latestSubmission {
        id
        createdAt
        status
        approvedEvidence
        reviewEvidence
        memberNote
        evidences {
          id
          createdAt
          consumption
          name
          file
          fileSizeFormatted
          fileUrl
          contentType
        }
        adminNote
        approvedDate
        approvedBy {
          id
          firstName
          lastName
          email
          phoneNumber
        }
      }
      expectedGenerationKwh
      totalApprovedConsumptionKwh
      totalUnapprovedConsumptionKwh
    }
  }
}

I have tried following through the examples in the repo to try and resolve this but cannot seem to work out if it is a bug or a misunderstanding..?

At present, the generated call throws multiple errors due to missing required values from the response

dapalex commented 11 months ago

Hi,

Thanks for your feedback.

I downloaded your project, found the query you are mentioning here and had a quick look at the code generated which seems to reflect correctly the rippleenergy schema you shared.

Using a mapped operation in its simplest way (in your case Me()) generates a query containing all fields and inner types it has in the schema definition.

Taking as example member inner type, you were expecting the following

    member {
      id
      registrationCompleted
      __typename
    }

but actually it is composed of many more fields (as per the generated code )

class Member(GQLObject):
   id: ID
   registrationCompleted: bool
   communityName: str
   isBusiness: bool
   businessName: str
   businessRegistrationNumber: str
   fullyChargedPreregistered: bool
   badges: list_Badge[Badge]
   branding: Branding
   beneficiaries: list_MemberNextOfKin[MemberNextOfKin]
   address: MemberAddress
   consumption: MemberConsumption
   invoices: list_MemberInvoice[MemberInvoice]
   memberships: list_GQLObject[GQLObject] ## Circular Reference for CoopMembership
   capacityOwnedTotal: BigInt
   ownershipPercentageTotal: float
   memberDocument: MemberDocument
   investmentTotal: int
   billSavingsAcrossAllProjects: int
   expectedGenerationAnnualTotal: BigInt
   hasReservedInActiveCoop: bool
   dateOfReservationInActiveCoop: Date
   dateOfLastSharePurchase: Date
   hasBoughtSharesInActiveCoop: bool
   hasBoughtSharesInGraigFatha: bool
   hasBoughtSharesInKirkHill: bool
   hasBoughtSharesInMultipleCoops: bool
   isSuppliedByOctopusEnergy: bool
   currentMemberSupplier: MemberSupplier
   waitingListPlaces: list_MemberCoopWaitingListPlace[MemberCoopWaitingListPlace]

Consequently the generated call is including mandatory arguments for its inner types.

In order to achieve the query you want there are several ways:

1) Using set_show function available in the query object (not very suitable for your situation, you should call it for each field)

Examples of usage can be found [here](https://github.com/dapalex/py-graphql-mapper/blob/develop/tests/tstquery/connobj_viewchange_test.py) or [here](https://github.com/dapalex/py-graphql-mapper/blob/develop/tests/tstquery/complex_obj_test.py)
Documentation [here](https://github.com/dapalex/py-graphql-mapper/tree/develop/pygqlmap#dynamic-visibility-of-fields)

2) Directly removing the fields you don't need in the generated class (if you are sure you won't ever need them)

3) Creating a custom type using pygqlmap module (which is a fairly simple implementation). An example of usage can be found here Documentation here