graphene-django custom get_queryset of DjangoObjectType does not work - graphene-django

in official docs: https://docs.graphene-python.org/projects/django/en/latest/queries/#default-queryset. In practice nothing happens. Please help. Below, for example i have just simple part of code with model Contact that have 3 fields(id, kind, value). I want the ContactType to always be filtered with kind='PHONE'.
class ContactType(DjangoObjectType):
class Meta:
model = Contact
#classmethod
def get_queryset(cls, queryset, info):
return queryset.filter(kind='PHONE')
class ContactQuery(graphene.ObjectType):
contacts = graphene.List(ContactType)
def resolve_contacts(self, info, **kwargs):
return Contact.objects.all()
The query:
{
contacts {
id
kind
value
}
}
returns:
{
"data": {
"contacts": [
{
"id": "1",
"kind": "INSTAGRAM",
"value": "technodomkz"
},
{
"id": "3",
"kind": "YOUTUBE",
"value": "UCF-HjvMMvgnhXhO4shk9i9Q"
},
{
"id": "4",
"kind": "FACEBOOK",
"value": "technodomkz"
},
{
"id": "5",
"kind": "TWITTER",
"value": "technodom_kz"
},
{
"id": "6",
"kind": "PHONE",
"value": "88000801111"
},
{
"id": "7",
"kind": "PHONE",
"value": "87272799999"
},
{
"id": "8",
"kind": "EMAIL",
"value": "callcenter#technodom.kz"
},
{
"id": "2",
"kind": "WEBSITE",
"value": "www.technodom.kz"
}
]
}
}
instead of:
{
"data": {
"contacts": [
{
"id": "6",
"kind": "PHONE",
"value": "88000801111"
},
{
"id": "7",
"kind": "PHONE",
"value": "87272799999"
}
]
}
}

For anyone looking for the answer to this question, it is answered here
Try swapping out List for DjangoConnectionField.
Looks like we need to document that get_queryset works with the DjangoConnectionField and that non-relay fields like List don't have the same magic.

Related

Schema definition in Swagger

I'm very new to OpenAPI and I'm using http://editor.swagger.io to design an API.
I'm stuck in Schema with a JSON looking like following
{
"CORRELATION_ID": "10",
"CONTROL":
{
"DAS_IS_RECIPIENT": "123",
"DOCTPYE": "ert",
"PROCESS_INDICATOR": "nord"
},
"HEADER":
{
"ID": "456",
"INVOICE_NUMBER": "678",
"DMS_DOC_ID": "876",
"INVOICE_DATE": "10082020"
},
"ITEMS": [
{
"SHORT_TEXT": "123",
"LSTAR": 0,
"QUANTITY": "23"
},
{
"SHORT_TEXT": "456",
"LSTAR": 234,
"QUANTITY": "21"
}
],
"DEBITOR":
{
"ID": "444",
"FIRSTNAME": "nick",
"LASTNAME": "cantre"
},
"CREDITOR":
{
"ID": "454",
"FIRSTNAME": "ava",
"LASTNAME": "pierre"
}
}
How to create a schema according to this JSON structure?

How to differ Site Page and Wiki Page in MS Graph or SharePoint REST API

I created a SharePoint Page in SitePages library using the new SharePoint online experience. The page was created and I tried to fetch the page properties using MS Graph endpoint:
https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/site_page.aspx?expand=listItem
The response I got:
{
...
"name": "site_page.aspx",
...
"file": {
"mimeType": "application/xml",
"hashes": {
"quickXorHash": ""
}
},
...
"listItem": {
...
"id": "4",
...
"contentType": {
"id": "0x0101009D1CB255DA76424F860D91F20E6C411800020BAE24978F3545AFD24007B325ACF9"
},
"fields": {
"FileLeafRef": "site_page.aspx",
"Title": "site_page",
"LinkTitle": "site_page",
"id": "4",
"ContentType": "Site Page",
...
}
}
}
Then I added Wiki Page content to this library to support Wiki pages in my site.
I created a new Wiki Page and fetched the page properties again using MS Graph endpoint:
https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/wiki_page.aspx?expand=listItem
The response I got:
{
...
"name": "wiki_page.aspx",
...
"file": {
"mimeType": "application/xml"
},
...
"listItem": {
...
"id": "5",
...
"contentType": {
"id": "0x0101009D1CB255DA76424F860D91F20E6C411800020BAE24978F3545AFD24007B325ACF9"
},
"fields": {
"FileLeafRef": "wiki_page.aspx",
"Title": "wiki_page",
"LinkTitle": "wiki_page",
"id": "5",
"ContentType": "Site Page",
"WikiField" : "..."
...
}
}
}
As you can see both items have the same Content Type. How that is possible?
The only difference being that the wiki_page Item doesn't include 'hashes' property and does include 'WikiField' property (the place where the wiki page content is stored).
Is there another way to distinguish page types?
You could try this endpoint: GET /sites/{site-id}/lists/{list-id}/items?expand=fields(select= ContentType)
My test result:
{
"#odata.etag": "\"b3b04ace-40cd-4847-a3d8-678bc658216d,2\"",
"createdDateTime": "2020-04-22T05:34:06Z",
"eTag": "\"b3b04ace-40cd-4847-a3d8-678bc658216d,2\"",
"id": "11",
"lastModifiedDateTime": "2020-04-22T05:34:06Z",
"webUrl": "https://xxxx.sharepoint.com/sites/dev/SitePages/wikipage.aspx",
"createdBy": {
"user": {
"email": "amos#xxxx.onmicrosoft.com",
"id": "fc1e9add-6f9c-4b95-83e1-a022441681d7",
"displayName": "test"
}
},
"lastModifiedBy": {
"user": {
"email": "amos#xxxx.onmicrosoft.com",
"id": "fc1e9add-6f9c-4b95-83e1-a022441681d7",
"displayName": "test"
}
},
"parentReference": {
"id": "79e13173-d5ee-4a17-a081-5c94d148f905",
"siteId": "xxxx.sharepoint.com,b57886ef-4c2a-4d56-ad29-27266638ac3b,b62d1450-8e6f-4be7-84a3-f6600fd6cc14"
},
"contentType": {
"id": "0x01010800511BB12BD4FB664A89516226DBDDF1FB"
},
"fields#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('siteid')/lists('63307f0b-bc1c-4372-bfea-6352ed57a0ff')/items('11')/fields/$entity",
"fields": {
"#odata.etag": "\"b3b04ace-40cd-4847-a3d8-678bc658216d,2\"",
"ContentType": "Wiki Page"
}
},
{
"#odata.etag": "\"70f12845-7646-4b2b-85bd-4a8074c105a0,1\"",
"createdDateTime": "2020-04-22T05:35:54Z",
"eTag": "\"70f12845-7646-4b2b-85bd-4a8074c105a0,1\"",
"id": "12",
"lastModifiedDateTime": "2020-04-22T05:35:54Z",
"webUrl": "https://xxxx.sharepoint.com/sites/dev/SitePages/webpartpage.aspx",
"createdBy": {
"user": {
"email": "amos#xxxx.onmicrosoft.com",
"id": "fc1e9add-6f9c-4b95-83e1-a022441681d7",
"displayName": "test"
}
},
"lastModifiedBy": {
"user": {
"email": "amos#xxxx.onmicrosoft.com",
"id": "fc1e9add-6f9c-4b95-83e1-a022441681d7",
"displayName": "test"
}
},
"parentReference": {
"id": "79e13173-d5ee-4a17-a081-5c94d148f905",
"siteId": "xxxx.sharepoint.com,b57886ef-4c2a-4d56-ad29-27266638ac3b,b62d1450-8e6f-4be7-84a3-f6600fd6cc14"
},
"contentType": {
"id": "0x0101090100FAC6DAD225005749BE7D6124B50B156E"
},
"fields#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('siteid')/lists('63307f0b-bc1c-4372-bfea-6352ed57a0ff')/items('12')/fields/$entity",
"fields": {
"#odata.etag": "\"70f12845-7646-4b2b-85bd-4a8074c105a0,1\"",
"ContentType": "Web Part Page"
}
}
You'd better check the type of your page.
Updatded:
"fields#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('siteid')/lists('63307f0b-bc1c-4372-bfea-6352ed57a0ff')/items('13')/fields/$entity"
"fields": {
"#odata.etag": "\"18361706-416b-4a71-8d31-bce87c1a57e4,3\"",
"ContentType": "Site Page"
}

Defining Array of Objects in Swagger Documentation

I'm using swagger for quite a bit now, we have started documenting our code using it, in one place there's an API response which returns multiple objects in the included block.
Example:
{
"data": {
"id": "1",
"type": "schoolPositions",
"attributes": {
"description": "teases the students",
"mustHaves": "principle"
},
"relationships": {
"schoolLocation": {
"data": {
"id": "72",
"type": "schoolLocations"
}
},
"schoolCompensation": {
"data": {
"id": "75",
"type": "schoolCompensations"
}
},
"jobSpecs": {
"data": [
{
"id": "82",
"type": "schoolAttachments"
}
]
}
}
},
"included": [
{
"id": "72",
"type": "schoolLocations",
"attributes": {
"city": "Berhampore",
"state": "West Bengal",
"postalCode": "742101",
"country": "India",
"globalRegionId": 30,
"regionId": 683
}
},
{
"id": "75",
"type": "schoolCompensations",
"attributes": {
"salary": "",
"bonus": "",
"equity": "",
"currencyId": null,
"equityType": "percent",
"salaryDescription": null
}
},
{
"id": "82",
"type": "schoolAttachments",
"attributes": {
"attachmentType": "JobSpecificationAttachmentType",
"fileFileName": "vs.jpg",
"fileContentType": "image/jpeg",
"fileFileSize": 2410039,
"fileUpdatedAt": "2018-12-12T07:06:38Z",
"downloadUrl": "001-vs.jpg?1544598398",
"klass": "SchoolAttachments"
}
}
]
I have wasted an entire day on the internet and documentation trying to document the included part, but I'm going wrong somewhere
response 200 do
key :description, 'School Data'
schema do
property :data do
key :type, :array
items do
key :'$ref', :School
end
end
property :included do
key :type, :array
items do
key :'$ref', :SchoolLocations
key :'$ref', :SchoolCompensations
key :'$ref', :SchoolAttachments
end
end
end
end
This shows only the SchoolAttachments in the included part.
I have tried using allOff but it doesn't work.

Swagger array of different objects

For example I need to represent following structure in swagger yaml format:
"included": [
{
"type": "people",
"id": "42",
"attributes": {
"name": "John",
"age": 80,
"gender": "male"
}
}
]
With single object everything is fine:
included:
type: "array"
items:
type: object
properties:
type:
type: "string"
# and so on
The question is what if I need to describe something like:
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
I know that in OpenAPI it's possible ( aka swagger 3 ) and tried some workarounds in current version but with no luck.

How do I create (or update) a QBO BillPayment so that it has an unapplied payment amount?

I created a bill through the QuickBooks Online (QBO) web UI. Then I queried using the API (v3, documented here). The response:
{
"SyncToken": "16",
"domain": "QBO",
"VendorRef": {
"name": "MyVendor",
"value": "237"
},
"TxnDate": "2014-12-07",
"TotalAmt": 1.83,
"CurrencyRef": {
"name": "United States Dollar",
"value": "USD"
},
"PayType": "Check",
"PrivateNote": "test billpayment description (mod)",
"sparse": false,
"Line": [
{
"Amount": 1.22,
"LinkedTxn": [
{
"TxnId": "1412",
"TxnType": "Bill"
}
]
}
],
"Id": "1413",
"CheckPayment": {
"PrintStatus": "NeedToPrint",
"BankAccountRef": {
"name": "MyBankAcct#1234",
"value": "137"
}
},
"MetaData": {
"CreateTime": "2014-12-07T18:44:51-08:00",
"LastUpdatedTime": "2014-12-10T20:20:28-08:00"
}
}
As you can see, it has $0.61 of the TotalAmt unapplied to any Bills. (The other $1.22 is applied to Bill 1412.) Now, when I try to update this bill to change JUST the amount applied to the linked transaction, I get unexpected results.
Here's the update request body:
{
"SyncToken": "16",
"domain": "QBO",
"VendorRef": {
"name": "MyVendor",
"value": "237"
},
"TxnDate": "2014-12-07",
"TotalAmt": 1.83,
"CurrencyRef": {
"name": "United States Dollar",
"value": "USD"
},
"PayType": "Check",
"PrivateNote": "test billpayment description (mod)",
"sparse": false,
"Line": [
{
"Amount": 1.21,
"LinkedTxn": [
{
"TxnId": "1412",
"TxnType": "Bill"
}
]
}
],
"Id": "1413",
"CheckPayment": {
"PrintStatus": "NeedToPrint",
"BankAccountRef": {
"name": "MyBankAcct#1234",
"value": "137"
}
},
"MetaData": {
"CreateTime": "2014-12-07T18:44:51-08:00",
"LastUpdatedTime": "2014-12-10T20:20:28-08:00"
}
}
In the response, it appears that the API just basically can't handle the situation (even though it clearly existed when I created it in the web UI). Look what happens to the TotalAmt attribute! It's reduced to whatever the payment was in the LinkedTxn:
{
"BillPayment": {
"VendorRef": {
"value": "237",
"name": "MyVendor"
},
"PayType": "Check",
"CheckPayment": {
"BankAccountRef": {
"value": "137",
"name": "MyBankAcct#1234"
},
"PrintStatus": "NeedToPrint"
},
"TotalAmt": 1.21,
"domain": "QBO",
"sparse": false,
"Id": "1413",
"SyncToken": "17",
"MetaData": {
"CreateTime": "2014-12-07T18:44:51-08:00",
"LastUpdatedTime": "2014-12-10T20:25:02-08:00"
},
"TxnDate": "2014-12-07",
"CurrencyRef": {
"value": "USD",
"name": "United States Dollar"
},
"PrivateNote": "test billpayment description (mod)",
"Line": [
{
"Amount": 1.21,
"LinkedTxn": [
{
"TxnId": "1412",
"TxnType": "Bill"
}
]
}
]
},
"time": "2014-12-10T20:25:01.91-08:00"
}
I don't see discussion of this on the known issues page, so I'm pretty confused.
If you have any suggestions as to how to get this to do what I want it to, I thank you for your thoughts!

Resources